Back to Repositories

Testing JSON Prettification Implementation in Insomnia

This test suite validates the JSON prettification functionality in Insomnia, ensuring proper formatting of JSON data. It uses a fixture-based approach to test various JSON formatting scenarios systematically and comprehensively.

Test Coverage Overview

The test suite provides comprehensive coverage of JSON prettification scenarios by utilizing fixture files.

Key features tested include:
  • Dynamic test generation from fixture files
  • Input/output file pairs validation
  • Consistent indentation handling
  • Various JSON formatting patterns

Implementation Analysis

The implementation employs a data-driven testing approach using file system operations to load test fixtures. The suite leverages Vitest’s describe/it pattern for structured test organization, with dynamic test case generation based on input/output file pairs.

Technical patterns include:
  • File system traversal for test data
  • Regex-based file matching
  • String manipulation for test case naming
  • Direct file content comparison

Technical Details

Testing tools and configuration:
  • Vitest as the test runner
  • Node.js fs module for file operations
  • Path module for cross-platform compatibility
  • JSON fixture files for test data
  • UTF-8 encoding for file reading

Best Practices Demonstrated

The test suite exemplifies several testing best practices in its implementation.

Notable practices include:
  • Data-driven test case generation
  • Separation of test data from test logic
  • Consistent file naming conventions
  • Clear test case descriptions
  • Modular test organization

kong/insomnia

packages/insomnia/src/utils/prettify/json.test.ts

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

import { jsonPrettify } from './json';

describe('jsonPrettify()', () => {
  const basePath = path.join(__dirname, './fixtures');
  const files = fs.readdirSync(basePath);
  for (const file of files) {
    if (!file.match(/-input\.json$/)) {
      continue;
    }

    const slug = file.replace(/-input\.json$/, '');
    const name = slug.replace(/-/g, ' ');

    it(`handles ${name}`, () => {
      const input = fs.readFileSync(path.join(basePath, `${slug}-input.json`), 'utf8').trim();
      const output = fs.readFileSync(path.join(basePath, `${slug}-output.json`), 'utf8').trim();
      const result = jsonPrettify(input, '  ');
      expect(result).toBe(output);
    });
  }
});