Back to Repositories

Testing After-Response Script Workflow in Insomnia

This test suite validates after-response script functionality in Insomnia, focusing on test assertions, environment persistence, and transient variable handling. The tests ensure proper integration between Insomnia’s testing utilities and environment management system.

Test Coverage Overview

The test suite covers three main areas of after-response script functionality:
  • Integration between insomnia.test and insomnia.expect assertions
  • Environment and base environment persistence capabilities
  • Transient variable handling and verification
Edge cases include failed assertions and environment hierarchy verification.

Implementation Analysis

Tests utilize Playwright’s testing framework for UI automation, implementing a fixture-based approach for consistent test setup. The implementation leverages Playwright’s page object model and custom test fixtures to interact with Insomnia’s interface programmatically.

Key patterns include clipboard-based fixture loading, async/await for UI interactions, and explicit wait conditions for response verification.

Technical Details

Testing tools and configuration:
  • Playwright test runner and assertion library
  • Custom fixtures for application state management
  • YAML-based test fixtures for request collections
  • Platform-specific test speed adjustments
  • Explicit selectors using data-test-id attributes

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Consistent beforeEach setup for test isolation
  • Explicit wait conditions for UI state changes
  • Clear test case organization and naming
  • Robust selector strategies using accessibility labels and test IDs
  • Comprehensive verification of both UI state and underlying data

kong/insomnia

packages/insomnia-smoke-test/tests/smoke/after-response-script-features.test.ts

            
import { expect } from '@playwright/test';

import { loadFixture } from '../../playwright/paths';
import { test } from '../../playwright/test';;

test.describe('after-response script features tests', async () => {
    test.slow(process.platform === 'darwin' || process.platform === 'win32', 'Slow app start on these platforms');

    test.beforeEach(async ({ app, page }) => {
        const text = await loadFixture('after-response-collection.yaml');
        await app.evaluate(async ({ clipboard }, text) => clipboard.writeText(text), text);

        await page.getByLabel('Import').click();
        await page.locator('[data-test-id="import-from-clipboard"]').click();
        await page.getByRole('button', { name: 'Scan' }).click();
        await page.getByRole('dialog').getByRole('button', { name: 'Import' }).click();

        await page.getByLabel('After-response Scripts').click();
    });

    test('post: insomnia.test and insomnia.expect can work together', async ({ page }) => {
        await page.getByLabel('Request Collection').getByTestId('tests with expect and test').press('Enter');

        // send
        await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click();

        // verify
        await page.getByRole('tab', { name: 'Tests' }).click();

        const responsePane = page.getByTestId('response-pane');
        await expect(responsePane).toContainText('PASS');
        await expect(responsePane).toContainText('FAILunhappy tests | AssertionError: expected 199 to deeply equal 200After-response Test');
    });

    test('environment and baseEnvironment can be persisted', async ({ page }) => {
        const statusTag = page.locator('[data-testid="response-status-tag"]:visible');
        await page.getByLabel('Request Collection').getByTestId('persist environments').press('Enter');

        // send
        await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click();

        // verify response
        await page.waitForSelector('[data-testid="response-status-tag"]:visible');
        await expect(statusTag).toContainText('200 OK');

        // verify persisted environment
        await page.getByRole('button', { name: 'Manage Environments' }).click();
        await page.getByRole('button', { name: 'Manage collection environments' }).click();
        const responseBody = page.getByRole('dialog').getByTestId('CodeEditor').locator('.CodeMirror-line');
        const rows = await responseBody.allInnerTexts();
        const bodyJson = JSON.parse(rows.join(' '));

        expect(bodyJson).toEqual({
            // no environment is selected so the environment value will be persisted to the base environment
            '__fromAfterScript1': 'baseEnvironment',
            '__fromAfterScript2': 'collection',
            '__fromAfterScript': 'environment',
        });
    });

    test('set transient var', async ({ page }) => {
        const statusTag = page.locator('[data-testid="response-status-tag"]:visible');
        await page.getByLabel('Request Collection').getByTestId('transient var').press('Enter');

        // send
        await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click();

        // verify response
        await page.waitForSelector('[data-testid="response-status-tag"]:visible');
        await expect(statusTag).toContainText('200 OK');

        // verify
        await page.getByRole('tab', { name: 'Tests' }).click();

        const rows = page.getByTestId('test-result-row');
        await expect(rows.first()).toContainText('PASS');
    });
});