Back to Repositories

Validating Environment Variable Key Formatting in Insomnia

This test suite validates environment editor utilities in Insomnia, focusing on key validation and nested object handling. It ensures proper formatting of environment variables and maintains data integrity within the application.

Test Coverage Overview

The test suite provides comprehensive coverage for environment variable key validation and nested object checking.

  • Tests key validation rules for $ prefixes and dot notation
  • Validates reserved keywords and root-level restrictions
  • Verifies nested object structure integrity
  • Covers array handling within environment objects

Implementation Analysis

The implementation uses Jest’s parameterized testing with it.each() for thorough validation scenarios.

Testing patterns include:
  • Systematic validation of invalid/valid key formats
  • Nested object traversal verification
  • Complex data structure handling with arrays and objects

Technical Details

Testing stack includes:
  • Vitest as the test runner
  • Jest-style assertions and matchers
  • Custom utility functions: ensureKeyIsValid and checkNestedKeys
  • Environment constants and templates integration

Best Practices Demonstrated

The test suite exemplifies strong testing practices through organized and systematic validation.

  • Comprehensive edge case coverage
  • Clear test descriptions using template literals
  • Consistent error message validation
  • Modular test organization with describe blocks

kong/insomnia

packages/insomnia/src/ui/components/editors/__tests__/environment-editor.test.ts

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

import { NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME } from '../../../../templating';
import { checkNestedKeys, ensureKeyIsValid } from '../environment-utils';

describe('ensureKeyIsValid()', () => {
  it.each([
    '$',
    '$a',
    '$ab',
  ])('"%s" should be invalid when key begins with $', key => {
    expect(ensureKeyIsValid(key, false)).toBe(`"${key}" cannot begin with '$' or contain a '.'`);
  });

  it.each([
    '.',
    'a.',
    '.a',
    'a.b',
  ])('"%s" should be invalid when key contains .', key => {
    expect(ensureKeyIsValid(key, false)).toBe(`"${key}" cannot begin with '$' or contain a '.'`);
  });

  it.each([
    '$a.b',
    '$.',
  ])('"%s" should be invalid when key starts with $ and contains .', key => {
    expect(ensureKeyIsValid(key, false)).toBe(`"${key}" cannot begin with '$' or contain a '.'`);
  });

  it.each([
    '_',
  ])('"%s" should be invalid when key is _', key => {
    expect(ensureKeyIsValid(key, true)).toBe(`"${key}" is a reserved key`);
  });

  it.each([
    '_',
    'a',
    'ab',
    'a$',
    'a$b',
    'a-b',
    `a${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}b`,
    `${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}ab`,
  ])('"%s" should be valid as a nested key', key => {
    expect(ensureKeyIsValid(key, false)).toBe(null);
  });

  it.each([
    'a',
    'ab',
    'a$',
    'a$b',
    'a-b',
    `a${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}b`,
    `${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}ab`,
  ])('"%s" should be valid as a root value', key => {
    expect(ensureKeyIsValid(key, true)).toBe(null);
  });
});

describe('checkNestedKeys()', () => {
  it('should check root property and error', () => {
    const obj = {
      'base-url': 'https://api.insomnia.rest',
      '$nes.ted': {
        'path-with-hyphens': '/path-with-hyphen',
      },
      'ar-ray': [
        '/first',
        {
          'second': 'second',
        },
        {
          'third': 'third',
        },
      ],
    };

    const err = checkNestedKeys(obj);

    expect(err).toBe('"$nes.ted" cannot begin with \'$\' or contain a \'.\'');
  });

  it('should check nested property and error', () => {
    const obj = {
      'base-url': 'https://api.insomnia.rest',
      'nested': {
        '$path-wi.th-hyphens': '/path-with-hyphen',
      },
      'ar-ray': [
        '/first',
        {
          'second': 'second',
        },
        {
          'third': 'third',
        },
      ],
    };

    const err = checkNestedKeys(obj);

    expect(err).toBe('"$path-wi.th-hyphens" cannot begin with \'$\' or contain a \'.\'');
  });

  it('should check for complex objects inside array', () => {
    const obj = {
      'base-url': 'https://api.insomnia.rest',
      'nested': {
        'path-with-hyphens': '/path-with-hyphen',
      },
      'ar-ray': [
        '/first',
        {
          'second': 'second',
        },
        {
          'thi.rd': 'third',
        },
      ],
    };

    const err = checkNestedKeys(obj);

    expect(err).toBe('"thi.rd" cannot begin with \'$\' or contain a \'.\'');
  });

  it('should check nested properties and pass', () => {
    const obj = {
      'base-url': 'https://api.insomnia.rest',
      'nested': {
        'path-with-hyphens': '/path-with-hyphen',
      },
      'ar-ray': [
        '/first',
        {
          'second': 'second',
        },
        {
          'third': 'third',
        },
      ],
    };

    const err = checkNestedKeys(obj);

    expect(err).toBe(null);
  });
});