Back to Repositories

Testing OpenAPI Specification Export with Kong Annotations in Insomnia

This test suite validates the OpenAPI specification export functionality in Insomnia, focusing on handling Kong-specific annotations. It ensures proper handling of x-kong annotations during specification export and validates the skipAnnotations flag behavior.

Test Coverage Overview

The test suite provides comprehensive coverage of the exportSpecification function, specifically focusing on Kong annotation handling.

  • Tests x-kong-plugin-oidc annotation retention when skipAnnotations is false
  • Verifies complete removal of Kong annotations when skipAnnotations is true
  • Validates OpenAPI 3.0.1 specification structure integrity

Implementation Analysis

The tests utilize Jest’s asynchronous testing capabilities with async/await patterns for handling the specification export process. The implementation employs string comparison to verify the exact output format and content preservation.

  • Async function testing with expect().toBe()
  • Direct string comparison for specification validation
  • Template literal usage for test data definition

Technical Details

  • Testing Framework: Jest/Vitest
  • Test Type: Unit Tests
  • Key Dependencies: exportSpecification function
  • Test Data: OpenAPI 3.0.1 specification with Kong plugins

Best Practices Demonstrated

The test suite exemplifies several testing best practices for API specification handling.

  • Isolated test cases with clear assertions
  • Comprehensive input/output validation
  • Well-structured test data setup
  • Clear test case descriptions
  • Proper async/await usage

kong/insomnia

packages/insomnia-inso/src/commands/export-specification.test.ts

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

import { exportSpecification } from './export-specification';

describe('exportSpecification()', () => {
  const withKongTags = `openapi: 3.0.1
info:
  description: Description
  version: 1.0.0
  title: API
servers:
  - url: https://api.insomnia.rest
paths:
  /path:
    x-kong-plugin-oidc:
      name: oidc
      enabled: true
      config:
        key_names: [api_key, apikey]
        key_in_body: false
        hide_credentials: true
    get:
      description: test
      responses:
        "200":
          description: OK`;
  it('should not remove all x-kong annotations from spec if skipAnnotations false', async () => {
    const result = await exportSpecification({ specContent: withKongTags, skipAnnotations: false });
    expect(result).toBe(withKongTags);
  });

  it('should remove all x-kong annotations from spec if skipAnnotations true', async () => {
    const result = await exportSpecification({ specContent: withKongTags, skipAnnotations: true });
    expect(result).toBe(`openapi: 3.0.1
info:
  description: Description
  version: 1.0.0
  title: API
servers:
  - url: https://api.insomnia.rest
paths:
  /path:
    get:
      description: test
      responses:
        "200":
          description: OK
`);
  });

});