Back to Repositories

Testing Configuration File Processing in Kong/insomnia

This test suite validates the functionality of reading and parsing Insomnia configuration files within the Kong/insomnia repository. It focuses on the tryToReadInsoConfigFile function, which handles various scenarios including successful config loading, missing files, and invalid configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of configuration file handling scenarios:
  • Loading valid YAML configuration files
  • Handling missing configuration files
  • Processing blank configuration files
  • Managing configurations with missing properties
Integration points include file system operations and console output handling.

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern with async/await for file operations. The implementation leverages vi for mocking and spy functionality, particularly for console output interception. The tests demonstrate proper path handling and configuration object validation patterns specific to TypeScript testing.

Technical Details

Testing tools and setup:
  • Vitest for test execution and assertions
  • Path module for cross-platform file path handling
  • Cosmiconfig for configuration file processing
  • Mock implementations for console methods
  • Fixture files for various test scenarios

Best Practices Demonstrated

The test suite exhibits several testing best practices:
  • Isolated test cases with clear, specific assertions
  • Proper mock handling and cleanup
  • Comprehensive error case coverage
  • Consistent use of async/await patterns
  • Well-organized test structure with descriptive naming

kong/insomnia

packages/insomnia-inso/src/get-options.test.ts

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

import { tryToReadInsoConfigFile } from './cli';

vi.unmock('cosmiconfig');

const fixturesDir = path.join('src', 'fixtures');

describe('tryToReadInsoConfigFile()', () => {
  it('should load .insorc-test.yaml config file in fixtures dir', async () => {
    const result = await tryToReadInsoConfigFile(path.join(fixturesDir, '.insorc-test.yaml'));
    expect(result).toEqual({
      options: {
      },
      scripts: {
        exportSpec: 'inso export spec',
        lintSpec: 'inso lint spec',
      },
      filePath: path.resolve(fixturesDir, '.insorc-test.yaml'),
    });
    expect(result?.options?.shouldBeIgnored).toBe(undefined);
  });

  it('should return empty object and report error if specified config file not found', async () => {
    const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
    const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
    const result = await tryToReadInsoConfigFile('not-found.yaml');
    expect(result).toEqual({});
    expect(consoleLogSpy).toHaveBeenCalledWith('Could not find config file at not-found.yaml.');
    expect(consoleErrorSpy).toHaveBeenCalled();
  });

  it('should return empty object if config file is blank', async () => {
    const result = await tryToReadInsoConfigFile(path.join(fixturesDir, '.insorc-blank.yaml'));
    expect(result).toEqual({});
  });

  it('should return blank properties and ignore extra items if settings and scripts not found in file', async () => {
    const result = await tryToReadInsoConfigFile(path.join(fixturesDir, '.insorc-missing-properties.yaml'));
    expect(result).toEqual({
      options: {},
      scripts: {},
      filePath: path.resolve(fixturesDir, '.insorc-missing-properties.yaml'),
    });
  });
});