Back to Repositories

Testing Import Conversion and Key Name Validation in Insomnia

This test suite validates the import functionality and key name validation in the Insomnia project. It focuses on testing error handling during file imports and the behavior of the dotInKeyNameInvariant function for detecting invalid key names.

Test Coverage Overview

The test suite provides comprehensive coverage of import error handling and key name validation.

Key areas tested include:
  • Import failure scenarios and error messages
  • Key name validation with dot notation
  • Edge cases for various input structures
  • Array and object handling in validation

Implementation Analysis

The testing approach utilizes Vitest framework with Jest-style assertions for robust validation. The implementation follows a structured pattern using describe/it blocks for clear test organization.

Technical patterns include:
  • Try-catch error validation
  • Parameterized testing using forEach
  • Type-safe error handling with TypeScript

Technical Details

Testing tools and configuration:
  • Vitest as the test runner
  • TypeScript for type safety
  • Jest-compatible expect assertions
  • Node.js assert module for fail cases

Best Practices Demonstrated

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

Notable practices include:
  • Descriptive test case naming
  • Proper error handling validation
  • Structured test organization
  • Comprehensive edge case coverage

kong/insomnia

packages/insomnia/src/utils/importers/__tests__/convert.test.ts

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

import { convert, dotInKeyNameInvariant } from '../convert';

describe('Import errors', () => {
  it('fail to find importer', async () => {
    try {
      await convert('foo');
      fail('Should have thrown error');
    } catch (err) {
      expect(err.message).toBe('No importers found for file');
    }
  });
});

describe('test dotInKeyNameInvariant', () => {
  [
    {
      input: {
        '.hehe': 'haha',
      },
      noError: false,
    },
    {
      input: {
        '.': '',
        'arr': [''],
      },
      noError: false,
    },
    {
      input: [
        '',
        1,
      ],
      noError: true,
    },
  ].forEach(testCase => {
    it(`check: ${testCase.input}`, () => {
      let e: Error | undefined = undefined;

      try {
        dotInKeyNameInvariant(testCase.input);
      } catch (ex) {
        e = ex;
      }
      expect(e === undefined).toBe(testCase.noError);
    });
  });
});