API Reference
API reference for setting up and managing unit tests with Suites.
Core APIs
- TestBed.solitary() - Create isolated unit tests where all dependencies are automatically mocked
- TestBed.sociable() - Test business logic interactions with
.boundaries()v4.0.0+ or.expose() - Mock Configuration - Configure mock behavior with
.mock().final()and.mock().impl() - mock() and stub() - Create standalone mocks outside TestBed
- UnitReference - Access mocked dependencies in tests
- Types - TypeScript type definitions
- Fail-Fast Behavior v4.0.0+ - Prevent false positives
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