Back to Repositories

Testing Environment Variable Management Implementation in Insomnia SDK

This test suite validates the Variables object functionality in the Insomnia SDK, focusing on environment variable handling and value replacement operations. The tests ensure proper variable scoping, override behavior, and string manipulation with UUID generation.

Test Coverage Overview

The test suite provides comprehensive coverage of the Variables object functionality.

Key areas tested include:
  • Basic variable operations and string replacement
  • UUID generation and validation
  • Environment variable scoping and override hierarchy
  • Edge cases with bracket handling and string formatting

Implementation Analysis

The testing approach utilizes Jest’s describe/it blocks for structured test organization. Tests implement a combination of direct object instantiation and method invocation patterns, with strong emphasis on environment variable hierarchy validation.

Technical implementation includes:
  • UUID validation for generated values
  • Multiple environment layer testing
  • Variable scope precedence verification

Technical Details

Testing infrastructure includes:
  • Vitest as the testing framework
  • UUID validation library
  • Environment and Variables class implementations
  • Custom test fixtures for different environment configurations

Best Practices Demonstrated

The test suite exemplifies several testing best practices in TypeScript unit testing.

Notable practices include:
  • Isolated test cases with clear objectives
  • Comprehensive environment setup scenarios
  • Explicit assertion statements
  • Progressive complexity in test cases
  • Clear separation of concerns between different variable scopes

kong/insomnia

packages/insomnia-sdk/src/objects/__tests__/environments.test.ts

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

import { Environment, Variables } from '../environments';

describe('test Variables object', () => {
    it('test basic operations', () => {
        const variables = new Variables({
            globalVars: new Environment('globals', { value: '777' }),
            environmentVars: new Environment('environments', {}),
            collectionVars: new Environment('baseEnvironment', {}),
            iterationDataVars: new Environment('iterationData', {}),
            localVars: new Environment('local', {}),
        });

        const uuidAnd777 = variables.replaceIn('{{    $randomUUID }}{{value  }}');
        expect(validate(uuidAnd777.replace('777', ''))).toBeTruthy();

        const uuidAndBrackets1 = variables.replaceIn('{{    $randomUUID }}}}');
        expect(validate(uuidAndBrackets1.replace('}}', ''))).toBeTruthy();

        const uuidAndBrackets2 = variables.replaceIn('}}{{    $randomUUID }}');
        expect(validate(uuidAndBrackets2.replace('}}', ''))).toBeTruthy();
    });

    it('test environment override', () => {
        const globalOnlyVariables = new Variables({
            globalVars: new Environment('globals', { scope: 'global', value: 'global-value' }),
            environmentVars: new Environment('environments', {}),
            collectionVars: new Environment('baseEnvironment', {}),
            iterationDataVars: new Environment('iterationData', {}),
            localVars: new Environment('local', {}),
        });
        const normalVariables = new Variables({
            globalVars: new Environment('globals', { scope: 'global', value: 'global-value' }),
            environmentVars: new Environment('environments', { scope: 'subEnv', value: 'subEnv-value' }),
            collectionVars: new Environment('baseEnvironment', { scope: 'baseEnv', value: 'baseEnv-value' }),
            iterationDataVars: new Environment('iterationData', {}),
            localVars: new Environment('local', {}),
        });
        const variablesWithIterationData = new Variables({
            globalVars: new Environment('globals', { scope: 'global', value: 'global-value' }),
            environmentVars: new Environment('environments', { scope: 'subEnv', value: 'subEnv-value' }),
            collectionVars: new Environment('baseEnvironment', { scope: 'baseEnv', value: 'baseEnv-value' }),
            iterationDataVars: new Environment('iterationData', { scope: 'iterationData', value: 'iterationData-value' }),
            localVars: new Environment('local', {}),
        });
        const variablesWithLocalData = new Variables({
            globalVars: new Environment('globals', { scope: 'global', value: 'global-value' }),
            environmentVars: new Environment('environments', { scope: 'subEnv', value: 'subEnv-value' }),
            collectionVars: new Environment('baseEnvironment', { scope: 'baseEnv', value: 'baseEnv-value' }),
            iterationDataVars: new Environment('iterationData', { scope: 'iterationData', value: 'iterationData-value' }),
            localVars: new Environment('local', { scope: 'local', value: 'local-value' }),
        });

        expect(globalOnlyVariables.get('value')).toEqual('global-value');
        expect(normalVariables.get('value')).toEqual('subEnv-value');
        expect(variablesWithIterationData.get('value')).toEqual('iterationData-value');
        expect(variablesWithLocalData.get('value')).toEqual('local-value');
    });
});