Back to Repositories

Testing Mocha Test Runner Implementation in Insomnia

This test suite validates the core functionality of running Mocha test suites within the Insomnia testing framework. It focuses on test execution, request handling, and error management while ensuring proper integration with the Insomnia API client.

Test Coverage Overview

The test suite provides comprehensive coverage of the Mocha test runner implementation.

Key areas tested include:
  • Basic test suite execution with passing and failing tests
  • Empty test suite handling
  • Multiple test file execution
  • Request callback integration
  • Invalid JavaScript error handling

Implementation Analysis

The testing approach utilizes Vitest for running unit tests, with mock implementations for request handling. The suite employs mock functions via vi.fn() to simulate HTTP requests and validates both successful and error scenarios.

Notable patterns include:
  • Async/await test implementations
  • Mock function injection
  • Statistics validation
  • Error case handling

Technical Details

Testing tools and configuration:
  • Vitest as the test runner
  • Chai assertion library integration
  • Mock request handler implementation
  • TypeScript type definitions for request callbacks
  • Dynamic test suite execution

Best Practices Demonstrated

The test suite demonstrates several testing best practices for maintaining code quality.

Notable practices include:
  • Isolated test cases with clear assertions
  • Proper mock implementation and verification
  • Comprehensive error handling coverage
  • Clean test organization with descriptive names
  • Type-safe implementations with TypeScript

kong/insomnia

packages/insomnia-testing/src/run/run.test.ts

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

import type { SendRequestCallback } from './insomnia';
import { runTests } from './run';

const exampleTest = `
const { expect } = chai;
describe('Example', () => {
  it('should be true', async () => expect(true).to.equal(true));
  it('should fail', async () => expect(true).to.equal(false));
});
`;

const exampleTestWithRequest = `
const { expect } = chai;
describe('Example', () => {
  it('should be true', async () => {
    const resp = await insomnia.send('req_123');
    expect(resp.status).to.equal(200);
  });
});
`;

const exampleEmptySuite = `
const { expect } = chai;
describe('Example', () => {
});
`;

describe('run', () => {
  const getMockedSendRequest = () => vi.fn<SendRequestCallback<{ status: number }>>().mockResolvedValue({ status: 200 });

  it('runs a mocha suite', async () => {
    const { stats } = await runTests(exampleTest, { sendRequest: getMockedSendRequest() });
    expect(stats.passes).toBe(1);
    expect(stats.tests).toBe(2);
    expect(stats.failures).toBe(1);
  });

  it('runs empty mocha suite', async () => {
    const { stats } = await runTests(exampleEmptySuite, { sendRequest: getMockedSendRequest() });
    expect(stats.passes).toBe(0);
    expect(stats.tests).toBe(0);
    expect(stats.failures).toBe(0);
  });

  it('works on multiple files', async () => {
    const { stats } = await runTests([exampleTest, exampleTest], { sendRequest: getMockedSendRequest() });
    expect(stats.passes).toBe(2);
    expect(stats.tests).toBe(4);
    expect(stats.failures).toBe(2);
  });

  it('calls sendRequest() callback', async () => {
    const sendRequest = getMockedSendRequest();

    const { stats } = await runTests(
      exampleTestWithRequest,
      { sendRequest },
    );

    expect(sendRequest).toHaveBeenCalledWith('req_123');
    expect(stats.passes).toBe(1);
  });

  it('throws on invalid JavaScript', async () => {
    let err;

    try {
      await runTests('this is invalid', { sendRequest: getMockedSendRequest() });
    } catch (e) {
      err = e;
    }

    expect(err).not.toBeNull();
  });
});