Back to Repositories

Testing Plugin Store Operations in Insomnia

This test suite validates the functionality of a plugin store implementation in Insomnia, focusing on data storage operations and state management. The tests ensure proper initialization and verify core storage methods including setting, getting, and removing items.

Test Coverage Overview

The test suite provides comprehensive coverage of the plugin store’s core functionality.

Key areas tested include:
  • Plugin initialization and store setup
  • Basic CRUD operations (set, get, remove items)
  • Bulk operations (clear, get all items)
  • Edge cases with non-existent keys

Implementation Analysis

The testing approach utilizes Jest’s describe/it blocks for organized test grouping and async/await patterns for handling asynchronous store operations.

Notable implementation patterns include:
  • Modular test organization with separate initialization and operation tests
  • Async function testing with proper Promise handling
  • Systematic verification of store method signatures

Technical Details

Testing infrastructure includes:
  • Vitest as the testing framework
  • Jest-style assertions (expect)
  • Mock plugin configuration for isolated testing
  • Async/await for handling store operations
  • Array sorting for deterministic comparisons

Best Practices Demonstrated

The test suite exemplifies several testing best practices for plugin architecture.

Notable practices include:
  • Isolated test cases with clear setup and teardown
  • Comprehensive method coverage
  • Explicit state verification
  • Consistent assertion patterns
  • Clear test case organization and naming

kong/insomnia

packages/insomnia/src/plugins/context/__tests__/store.test.ts

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

import * as plugin from '../store';
const PLUGIN = {
  name: 'my-plugin',
  version: '1.0.0',
  directory: '/plugins/my-plugin',
  module: {},
};

describe('init()', () => {

  it('initializes correctly', async () => {
    const result = plugin.init({
      name: PLUGIN,
    });
    expect(Object.keys(result.store).sort()).toEqual([
      'all',
      'clear',
      'getItem',
      'hasItem',
      'removeItem',
      'setItem',
    ]);
  });
});

describe('store.*', () => {

  it('all methods work', async () => {
    const p = plugin.init(PLUGIN);
    // Null item for no result
    expect(await p.store.getItem('unset-key')).toBeNull();
    // Add something
    await p.store.setItem('color', 'blue');
    expect(await p.store.getItem('color')).toBe('blue');
    expect(await p.store.hasItem('color')).toBe(true);
    // Remove something
    await p.store.removeItem('color');
    expect(await p.store.hasItem('color')).toBe(false);
    expect(await p.store.getItem('color')).toBeNull();
    // Add some more
    await p.store.setItem('a', 'aaa');
    await p.store.setItem('b', 'bbb');
    await p.store.setItem('c', 'ccc');
    const all = await p.store.all();
    expect(all.sort((a, b) => (a.key < b.key ? -1 : 1))).toEqual([
      {
        key: 'a',
        value: 'aaa',
      },
      {
        key: 'b',
        value: 'bbb',
      },
      {
        key: 'c',
        value: 'ccc',
      },
    ]);
    // Clear it
    await p.store.clear();
    expect(await p.store.getItem('a')).toBeNull();
    expect(await p.store.getItem('b')).toBeNull();
    expect(await p.store.getItem('c')).toBeNull();
  });
});