Skip to main content

API Reference

API reference for setting up and managing unit tests with Suites.

Core APIs

Quick Reference

Creating a Solitary Test

const { unit, unitRef } = await TestBed.solitary(UserService).compile();

Creating a Sociable Test

// Recommended: boundaries (v4.0.0+)
const { unit, unitRef } = await TestBed.sociable(UserService)
.boundaries([ComplexService]) // List what to avoid
.compile();

// Alternative: expose
const { unit, unitRef } = await TestBed.sociable(UserService)
.expose(UserApi) // List what to keep
.compile();

Configuring Mocks

// Final configuration (immutable)
await TestBed.solitary(UserService)
.mock(UserApi)
.final({ getRandom: async () => ({ id: 1, name: "John" }) })
.compile();

// Flexible configuration
await TestBed.solitary(UserService)
.mock(UserApi)
.impl(stubFn => ({ getRandom: stubFn().mockResolvedValue({ id: 1 }) }))
.compile();

Creating Standalone Mocks

import { mock, stub } from "@suites/unit";

// Mock a full class
const userRepo = mock<UserRepository>();
userRepo.findById.mockResolvedValue(testUser);

// Create a stub function
const stubFn = stub();
stubFn.mockReturnValue(42);

Terminology

  • Mock: A complete replacement of a dependency class where each method has been replaced with a stub
  • Stub: An individual method replacement that provides predefined responses
  • Mocked<T>: The type representing a mocked dependency with stubbed methods