Back to Repositories

Validating OpenAPI Specification Linting in Insomnia

This test suite validates the OpenAPI specification linting functionality in Insomnia’s CLI tool. It ensures proper validation of OpenAPI documents against standard rules and custom rulesets, covering both valid and invalid specification scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the OpenAPI specification linting functionality.

  • Tests valid OpenAPI 3.0.2 specification validation
  • Verifies custom ruleset integration
  • Validates handling of malformed specifications
  • Tests error detection capabilities

Implementation Analysis

The testing approach utilizes Jest’s asynchronous testing capabilities to validate the lintSpecification function. The implementation employs async/await patterns for specification validation, with modular test cases that isolate specific validation scenarios.

  • Async test implementation using Jest
  • Modular test case organization
  • Mock specification content injection

Technical Details

  • Testing Framework: Jest/Vitest
  • File Type: TypeScript Test (.test.ts)
  • Test Utilities: path module for file resolution
  • Configuration: Custom ruleset loading capability
  • Mock Data: Sample OpenAPI specifications

Best Practices Demonstrated

The test suite exemplifies strong testing practices through clear test case organization and comprehensive validation scenarios.

  • Isolated test cases with clear purposes
  • Proper async/await usage
  • Meaningful test descriptions
  • Edge case handling
  • Structured mock data

kong/insomnia

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

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

import { lintSpecification } from './lint-specification';

describe('lint specification', () => {

  const specContent = `openapi: '3.0.2'
info:
  title: Sample Spec
  version: '1.2'
  description: A sample API specification
  contact:
    email: [email protected]
servers:
  - url: https://200.insomnia.rest
tags:
  - name: Folder
paths:
  /global:
    get:
      description: Global
      operationId: get_global
      tags:
        - Folder
      responses:
        '200':
          description: OK
  /override:
    get:
      description: Override
      operationId: get_override
      tags:
        - Folder
      responses:
        '200':
          description: OK`;

  it('should return true for linting passed', async () => {
    const result = await lintSpecification({ specContent });
    expect(result.isValid).toBe(true);
  });

  // TODO: fix;
  it.skip('should lint specification with custom ruleset', async () => {
    const rulesetFileName = path.join(process.cwd(), 'src/commands/fixtures/with-ruleset/.spectral.yaml');
    const result = await lintSpecification({
      specContent: `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
`, rulesetFileName,
    });
    expect(result.isValid).toBe(true);
  });

  it('should return false for linting failed', async () => {
    const badSpec = `openapi: '3.0.2'                                                                            
info:
  title: Global Security
  version: '1.2'
servers:
  - url: https://api.server.test/v1
tags:
  - name: Folder

  paths:
  /global:
    get:
      tags:
        - Folder
      responses:
        '200':
          description: OK
  /override:
    get:
      security:
        - Key-Query: []
      responses:
        '200':
          description: OK`;
    const result = await lintSpecification({ specContent: badSpec });
    expect(result.isValid).toBe(false);
  });
});