Back to Repositories

Testing Base64 Template Tag Encoding in Insomnia

This test suite validates the base64 template tag functionality in Insomnia’s templating system. It focuses on encoding capabilities and error handling for different input types and actions.

Test Coverage Overview

The test suite provides comprehensive coverage of base64 encoding operations:
  • Normal string to base64 encoding
  • Hex string to base64 encoding
  • Invalid action handling
  • Invalid encoding type validation

Implementation Analysis

The testing approach utilizes Vitest’s describe/it pattern for organized test grouping. Tests validate both successful encoding operations and error cases using TypeScript’s type system with PluginTemplateTagContext interface.

Implementation leverages Jest’s expect assertions for both positive and negative test cases.

Technical Details

Testing Framework: Vitest
  • TypeScript for type safety
  • Jest-style assertions
  • Invariant checks for tag existence
  • Mock PluginTemplateTagContext implementation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Nested describe blocks for logical grouping
  • Comprehensive error case coverage
  • Type-safe testing with TypeScript
  • Clear test case isolation
  • Consistent assertion patterns

kong/insomnia

packages/insomnia/src/ui/components/templating/__tests__/local-template-tags.test.ts

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

import { type PluginTemplateTagContext } from '../../../../templating/extensions';
import { invariant } from '../../../../utils/invariant';
import { localTemplateTags } from '../local-template-tags';
describe('base64 tag', () => {
  describe('encoder', () => {
    const base64EncoderTag = localTemplateTags.find(p => p.templateTag.name === 'base64')?.templateTag;
    invariant(base64EncoderTag, 'missing tag in localTemplateTags');
    it('encodes from normal', () => {
      const encoded = base64EncoderTag.run({} as PluginTemplateTagContext, 'encode', 'normal', 'hello');
      expect(encoded).toBe('aGVsbG8=');
    });
    it('encodes from hex', () => {
      const encoded = base64EncoderTag.run({} as PluginTemplateTagContext, 'encode', 'hex', 'abc123');
      expect(encoded).toBe('q8Ej');
    });
    it('errors on invalid action', () => {
      expect(() => base64EncoderTag.run({} as PluginTemplateTagContext, 'transform', 'normal', 'hello')).toThrowError();
    });
    it('errors on invalid kind', () => {
      expect(() => base64EncoderTag.run({} as PluginTemplateTagContext, 'encode', 'klingon', 'hello')).toThrowError();
    });
  });

});