Back to Repositories

Testing String Manipulation Utilities Implementation in Insomnia

This test suite validates core utility functions in Insomnia’s testing package, focusing on string indentation and JavaScript string escaping functionality. The tests ensure robust handling of code formatting and string manipulation operations essential for test generation.

Test Coverage Overview

The test suite provides comprehensive coverage of two utility functions: indent() and escapeJsStr().

Key functionality tested includes:
  • Indentation handling for single and multi-line text
  • Edge cases for zero and negative indentation values
  • String escaping for JavaScript quotes
  • Various string manipulation scenarios

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for clear test organization and hierarchical structure. Tests are implemented using TypeScript, leveraging strict typing and modern ES6+ features.

Technical patterns include:
  • Nested describe blocks for logical grouping
  • Multiple assertions per test case
  • Edge case validation
  • String literal testing with various quote styles

Technical Details

Testing infrastructure includes:
  • Vitest as the testing framework
  • TypeScript for type-safe testing
  • ESLint integration for code quality
  • Jest-style assertion syntax

Best Practices Demonstrated

The test suite exemplifies several testing best practices, including clear test case isolation and comprehensive edge case coverage. Each test focuses on a specific functionality aspect with descriptive naming.

Notable practices include:
  • Descriptive test naming conventions
  • Logical test organization
  • Comprehensive edge case handling
  • Clean and maintainable test structure

kong/insomnia

packages/insomnia-testing/src/generate/util.test.ts

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

import { escapeJsStr, indent } from './util';

describe('util', () => {
  describe('indent()', () => {
    it('skips indent on <= 0', () => {
      expect(indent(0, 'hello')).toBe('hello');
      expect(indent(-1, 'hello')).toBe('hello');
    });

    it('indents single lines', () => {
      expect(indent(1, 'hello')).toBe('  hello');
      expect(indent(3, 'hello')).toBe('      hello');
    });

    it('indents multi-line blocks', () => {
      const text = 'function greet() {\n  console.log(\'Hello World!\');\n}';
      expect(indent(1, text)).toBe(
        '  function greet() {\n    console.log(\'Hello World!\');\n  }',
      );
    });
  });

  describe('escapeJsStr()', () => {
    it('does not escape something without quotes', () => {
      expect(escapeJsStr('Hello World')).toBe('Hello World');
    });

    it('escapes something with quotes', () => {
      // eslint-disable-next-line @typescript-eslint/quotes -- want to test both quote styles
      expect(escapeJsStr(`"Hello" 'World'`)).toBe(`"Hello" \\'World\\'`);
    });
  });
});