Back to Repositories

Testing VCS Backend Project Initialization in Insomnia

This test suite validates the initialization and snapshot pushing functionality in the VCS (Version Control System) backend project. It focuses on testing the pushSnapshotOnInitialize function’s behavior under different project and workspace configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of the pushSnapshotOnInitialize function.

Key areas tested include:
  • Handling inactive projects
  • Processing non-remote projects
  • Workspace-project relationship validation
  • Successful snapshot pushing scenarios
Edge cases covered include null remote IDs, mismatched workspace-project relationships, and state persistence verification.

Implementation Analysis

The testing approach utilizes Jest’s describe/it blocks with mock implementations via Vitest. The tests employ spy functions to track method calls and validate execution paths.

Technical patterns include:
  • Mock reset between tests using beforeEach
  • Spy cleanup with afterAll
  • Async/await pattern for database operations
  • Expect assertions for both positive and negative cases

Technical Details

Testing tools and configuration:
  • Vitest for test execution and mocking
  • MemoryDriver for storage simulation
  • Models integration for project/workspace management
  • VCS class implementation for version control operations

Best Practices Demonstrated

The test suite demonstrates excellent testing practices with isolated test cases and thorough state management.

Notable practices include:
  • Clear test case isolation
  • Proper mock cleanup
  • Comprehensive assertion coverage
  • Consistent async/await usage
  • Meaningful test descriptions

kong/insomnia

packages/insomnia/src/sync/vcs/__tests__/initialize-backend-project.test.ts

            
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';

import * as models from '../../../models';
import MemoryDriver from '../../store/drivers/memory-driver';
import { pushSnapshotOnInitialize } from '../initialize-backend-project';
import { VCS } from '../vcs';

describe('initialize-backend-project', () => {

  describe('pushSnapshotOnInitialize()', () => {
    const vcs = new VCS(new MemoryDriver());

    const pushSpy = vi.spyOn(vcs, 'push');

    beforeEach(() => {
      vi.resetAllMocks();
    });

    afterAll(() => {
      pushSpy.mockClear();
    });

    it('should not push if no active project', async () => {
      const project = await models.project.create({ remoteId: null });
      const workspace = await models.workspace.create({ parentId: project._id });
      const workspaceMeta = await models.workspaceMeta.create({ parentId: workspace._id });
      vcs.clearBackendProject();

      await pushSnapshotOnInitialize({ vcs, project, workspace });

      expect(pushSpy).not.toHaveBeenCalled();
      await expect(models.workspaceMeta.getByParentId(workspace._id)).resolves.toStrictEqual(workspaceMeta);
    });

    it('should not push snapshot if not remote project', async () => {
      const project = await models.project.create({ remoteId: null });
      const workspace = await models.workspace.create({ parentId: project._id });
      const workspaceMeta = await models.workspaceMeta.create({ parentId: workspace._id });
      vcs.switchAndCreateBackendProjectIfNotExist(workspace._id, workspace.name);

      await pushSnapshotOnInitialize({ vcs, project, workspace });

      expect(pushSpy).not.toHaveBeenCalled();
      await expect(models.workspaceMeta.getByParentId(workspace._id)).resolves.toStrictEqual(workspaceMeta);
    });

    it('should not push snapshot if workspace not in project', async () => {
      const project = await models.project.create({ remoteId: 'abc' });
      const anotherProject = await models.project.create({ remoteId: 'def' });
      const workspace = await models.workspace.create({ parentId: anotherProject._id });
      vcs.switchAndCreateBackendProjectIfNotExist(workspace._id, workspace.name);

      await pushSnapshotOnInitialize({ vcs, project, workspace });

      expect(pushSpy).not.toHaveBeenCalled();
    });

    it('should push snapshot if conditions are met', async () => {
      const project = await models.project.create({ remoteId: 'abc', parentId: 'team_abc' });
      const workspace = await models.workspace.create({ parentId: project._id });
      await models.workspaceMeta.create({ parentId: workspace._id, pushSnapshotOnInitialize: true });
      vcs.switchAndCreateBackendProjectIfNotExist(workspace._id, workspace.name);

      await pushSnapshotOnInitialize({ vcs, project, workspace });

      expect(pushSpy).toHaveBeenCalledWith({ teamId: 'team_abc', teamProjectId: project.remoteId });
      const updatedMeta = await models.workspaceMeta.getByParentId(workspace._id);
      expect(updatedMeta?.pushSnapshotOnInitialize).toBe(false);
    });
  });
});