Back to Repositories

Testing Insomnia Database Adapter Implementation in Kong/insomnia

This test suite validates the Insomnia adapter functionality for database operations, focusing on data seeding and file handling capabilities. It ensures proper parsing and filtering of both JSON and YAML data files while maintaining data integrity across different formats.

Test Coverage Overview

The test suite provides comprehensive coverage of the Insomnia adapter’s core functionalities.

Key areas tested include:
  • JSON and YAML file processing
  • Data filtering capabilities
  • Error handling for malformed files
  • Invalid pathname scenarios
Integration points cover database seeding, file system interactions, and data structure validation.

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for structured test organization. The implementation leverages TypeScript’s type system for robust testing, employing async/await patterns for file operations and database interactions.

Framework-specific features include:
  • Jest’s it.each for parametrized testing
  • Snapshot testing for error cases
  • Async test handling
  • Type-safe expectations

Technical Details

Testing tools and configuration:
  • Vitest as the test runner
  • Path module for cross-platform file handling
  • TypeScript for type checking
  • Fixture-based test data
  • Snapshot testing for error validation

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through comprehensive coverage and robust implementation.

Notable practices include:
  • Isolated test cases with clear assertions
  • Consistent error handling validation
  • Parametrized testing for similar scenarios
  • Well-organized test fixtures
  • Clear test case descriptions

kong/insomnia

packages/insomnia-inso/src/db/adapters/insomnia-adapter.test.ts

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

import insomniaAdapter from './insomnia-adapter';

describe('insomniaAdapter()', () => {
  const fixturesPath = path.join(__dirname, '../fixtures');

  it('should seed with data file (JSON)', async () => {
    const pathname = path.join(fixturesPath, 'insomnia-v4', 'insomnia_v4.json');
    const db = await insomniaAdapter(pathname);

    expect(db?.ApiSpec.length).toBe(1);
    expect(db?.Environment.length).toBe(2);
    expect(db?.Request.length).toBe(13);
    expect(db?.RequestGroup.length).toBe(1);
    expect(db?.Workspace.length).toBe(1);
    expect(db?.UnitTestSuite.length).toBe(1);
    expect(db?.UnitTest.length).toBe(2);
  });

  it('should seed with data file and its filters (JSON)', async () => {
    const pathname = path.join(fixturesPath, 'insomnia-v4', 'insomnia_v4.json');
    const db = await insomniaAdapter(pathname, ['Environment']);

    expect(db?.ApiSpec.length).toBe(0);
    expect(db?.Environment.length).toBe(2);
    expect(db?.Request.length).toBe(0);
    expect(db?.RequestGroup.length).toBe(0);
    expect(db?.Workspace.length).toBe(0);
    expect(db?.UnitTestSuite.length).toBe(0);
    expect(db?.UnitTest.length).toBe(0);
  });

  it('should seed with data file (YAML)', async () => {
    const pathname = path.join(fixturesPath, 'insomnia-v4', 'insomnia_v4.yaml');
    const db = await insomniaAdapter(pathname);

    expect(db?.ApiSpec.length).toBe(1);
    expect(db?.Environment.length).toBe(2);
    expect(db?.Request.length).toBe(13);
    expect(db?.RequestGroup.length).toBe(1);
    expect(db?.Workspace.length).toBe(1);
    expect(db?.UnitTestSuite.length).toBe(1);
    expect(db?.UnitTest.length).toBe(2);
  });

  it('should seed with data file and its filters (YAML)', async () => {
    const pathname = path.join(fixturesPath, 'insomnia-v4', 'insomnia_v4.yaml');
    const db = await insomniaAdapter(pathname, ['Environment']);

    expect(db?.ApiSpec.length).toBe(0);
    expect(db?.Environment.length).toBe(2);
    expect(db?.Request.length).toBe(0);
    expect(db?.RequestGroup.length).toBe(0);
    expect(db?.Workspace.length).toBe(0);
    expect(db?.UnitTestSuite.length).toBe(0);
    expect(db?.UnitTest.length).toBe(0);
  });

  it.each([
    'malformed.yaml',
    'no-export-format.yaml',
    'v3-export-format.yaml',
    'empty.yaml',
  ])('should throw InsoError if malformed yaml content: %s', async (fileName: string) => {
    const pathname = path.join(fixturesPath, 'insomnia-v4', fileName);
    await expect(insomniaAdapter(pathname)).rejects.toThrowErrorMatchingSnapshot();
  });

  it('should return null if pathname is invalid', async () => {
    const pathname = path.join(fixturesPath, 'insomnia-v4', 'insomnia');
    const db = await insomniaAdapter(pathname);
    expect(db).toBe(null);
  });
});