Back to Repositories

Testing API Specification Parsing Implementation in Insomnia

This test suite validates the API specification parsing functionality in Insomnia, focusing on handling OpenAPI and Swagger formats in both YAML and JSON. The tests ensure robust parsing capabilities across different specification versions and error handling scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of API specification parsing scenarios:

  • OpenAPI 3.0.0 parsing in both YAML and JSON formats
  • Swagger 2.0.0 specification handling
  • Unknown specification format processing
  • Empty document handling
  • Malformed document error handling

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for structured test organization. Each test case validates the parseApiSpec function’s ability to correctly process different input formats and return properly structured output objects. The implementation leverages YAML parsing capabilities alongside JSON handling.

Technical Details

  • Testing Framework: Jest/Vitest
  • Key Dependencies: YAML library for specification parsing
  • Test Structure: Modular test cases with clear input/output validation
  • Error Handling: Explicit validation of error cases

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated test cases, comprehensive edge case coverage, and clear error scenario handling. The code organization follows a clear pattern of setup, execution, and assertion, with each test case focusing on a specific parsing scenario.

kong/insomnia

packages/insomnia/src/common/__tests__/api-specs.test.ts

            
import { describe, expect, it } from 'vitest';
import YAML from 'yaml';

import { parseApiSpec } from '../api-specs';

describe('parseApiSpec()', () => {
  it('parses YAML and JSON OpenAPI specs', () => {
    const objSpec = {
      openapi: '3.0.0',
      info: {
        title: 'My API',
      },
    };
    const yamlSpec = YAML.stringify(objSpec);
    const jsonSpec = JSON.stringify(objSpec);
    const expected = {
      format: 'openapi',
      formatVersion: '3.0.0',
      contents: objSpec,
    };
    expect(parseApiSpec(yamlSpec)).toEqual({ ...expected, rawContents: yamlSpec });
    expect(parseApiSpec(jsonSpec)).toEqual({ ...expected, rawContents: jsonSpec });
  });

  it('parses YAML and JSON Swagger specs', () => {
    const objSpec = {
      swagger: '2.0.0',
      info: {
        title: 'My API',
      },
    };
    const expected = {
      format: 'swagger',
      formatVersion: '2.0.0',
      contents: objSpec,
    };
    const yamlSpec = YAML.stringify(objSpec);
    const jsonSpec = JSON.stringify(objSpec);
    expect(parseApiSpec(yamlSpec)).toEqual({ ...expected, rawContents: yamlSpec });
    expect(parseApiSpec(jsonSpec)).toEqual({ ...expected, rawContents: jsonSpec });
  });

  it('parses YAML and JSON Unknown specs', () => {
    const objSpec = {
      funnyBusiness: '2.0.0',
      info: {
        title: 'My API',
      },
    };
    const expected = {
      format: null,
      formatVersion: null,
      contents: objSpec,
    };
    const yamlSpec = YAML.stringify(objSpec);
    const jsonSpec = JSON.stringify(objSpec);
    expect(parseApiSpec(yamlSpec)).toEqual({ ...expected, rawContents: yamlSpec });
    expect(parseApiSpec(jsonSpec)).toEqual({ ...expected, rawContents: jsonSpec });
  });

  it('returns the default result if empty document', () => {
    const expected = {
      format: null,
      formatVersion: null,
      contents: null,
      rawContents: '',
    };
    expect(parseApiSpec('')).toEqual(expected);
  });

  it('Fails on malformed JSON/YAML', () => {
    const rawSpec = ['openapi: 3.0.0', 'info: {{{'].join('\n');
    expect(() => parseApiSpec(rawSpec)).toThrowError('Failed to parse API spec');
  });
});