Class SpecAbstract

Description

Abstract base class for Vitest-based integration and unit test suites.

Provides:

  • HTTP API testing via supertest with request/response logging.
  • Unit-testing with snapshot-based argument and return-value matching.
  • Data validation using class-validator or custom validator functions.
  • Nock-based HTTP mocking.
  • Sinon / vi mocks for test doubles.

Example

import { Spec } from '@biorate/vitest-spec';
import { suite, test } from '@biorate/vitest';

@suite('My API Suite')
class MyApiSpec extends Spec {
protected get httpServer() {
return app.listen();
}

@test('GET /users returns 200')
async 'GET /users'() {
const res = await this.api().get('/users');
this.expect(res.status).toBe(200);
}
}

Constructors

Properties

#supertest: any
#unit: Unit = ...
testDir: string = 'tests'

Description

Directory name for test data and argument files.

Accessors

  • get httpServer(): any
  • Returns any

    Description

    Subclasses must override this getter to return the HTTP server instance (e.g. an Express/NestJS app) for supertest to bind to.

Methods

  • Parameters

    • Optional url: string

    Returns SuperTest<Test>

    Description

    Creates an API test helper (supertest proxy) with request/response logging attached.

  • Parameters

    • result: any
    • exp: any
    • Optional message: string

    Returns any

    Description

    Assert deep equality between the result and expected value. Wraps Vitest's expect(result).toEqual(exp).

  • Parameters

    • method: string
    • url: string
    • data: string

    Returns void

    Description

    Request-logging hook. Override to log outgoing HTTP requests. Default is a no-op.

  • Parameters

    • status: number
    • body: string

    Returns void

    Description

    Response-logging hook. Override to log incoming HTTP responses. Default is a no-op.

  • Parameters

    • options: IUnitOptions

    Returns Promise<any>

    Description

    Run a unit test: load arguments from disk, invoke the method, and run snapshot matching on args, context, and return value.

  • Parameters

    • options: IValidatorOptions

    Returns Promise<any>

    Description

    Validate data using a class-validator schema or a custom validator function.