Back to Repositories

Testing gRPC Request Object Creation in Insomnia

This test suite validates the initialization and creation of gRPC requests in the Insomnia API client. It ensures proper handling of request objects with required fields and validates error scenarios for missing parameters.

Test Coverage Overview

The test suite provides comprehensive coverage of gRPC request handling functionality.

Key areas tested include:
  • Request initialization with default values
  • Request creation with custom parameters
  • Validation of required fields
  • Error handling for missing parentId
The tests verify both successful scenarios and edge cases for request object creation.

Implementation Analysis

The testing approach utilizes Vitest’s describe/it blocks for structured test organization. Mock implementations are used for Date.now to ensure consistent timestamps across tests. The implementation leverages Jest-style assertions with expect().toEqual() for deep object comparison and expect().toThrow() for error validation.

Technical Details

Testing tools and configuration:
  • Vitest as the test runner
  • Vi mock functions for time manipulation
  • TypeScript for type safety
  • Object model validation for gRPC requests
  • Async/await pattern for database operations

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable practices include:
  • Isolated test cases with clear descriptions
  • Consistent use of mocking for deterministic results
  • Comprehensive object validation
  • Error case coverage
  • Clear separation of test scenarios

kong/insomnia

packages/insomnia/src/models/__tests__/grpc-request.test.ts

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

import * as models from '../index';

describe('init()', () => {
  it('contains all required fields', async () => {
    Date.now = vi.fn().mockReturnValue(1478795580200);
    expect(models.grpcRequest.init()).toEqual({
      url: '',
      name: 'New gRPC Request',
      description: '',
      protoFileId: '',
      protoMethodName: '',
      metadata: [],
      body: {
        text: '{}',
      },
      reflectionApi: {
        enabled: false,
        apiKey: '',
        module: 'buf.build/connectrpc/eliza',
        url: 'https://buf.build',
      },
      metaSortKey: -1478795580200,
      isPrivate: false,
    });
  });
});

describe('create()', () => {
  it('creates a valid GrpcRequest', async () => {
    Date.now = vi.fn().mockReturnValue(1478795580200);
    const request = await models.grpcRequest.create({
      name: 'My request',
      parentId: 'fld_124',
    });
    const expected = {
      _id: 'greq_cc1dd2ca4275747aa88199e8efd42403',
      created: 1478795580200,
      modified: 1478795580200,
      parentId: 'fld_124',
      name: 'My request',
      description: '',
      url: '',
      protoFileId: '',
      protoMethodName: '',
      metadata: [],
      body: {
        text: '{}',
      },
      reflectionApi: {
        enabled: false,
        apiKey: '',
        module: 'buf.build/connectrpc/eliza',
        url: 'https://buf.build',
      },
      metaSortKey: -1478795580200,
      isPrivate: false,
      type: 'GrpcRequest',
    };
    expect(request).toEqual(expected);
    expect(await models.grpcRequest.getById(expected._id)).toEqual(expected);
  });

  it('fails when missing parentId', async () => {
    Date.now = vi.fn().mockReturnValue(1478795580200);
    expect(() =>
      models.grpcRequest.create({
        name: 'no parentId',
      }),
    ).toThrow('New GrpcRequest missing `parentId`');
  });
});