Skip to main content
Version: Next

Suites

Unit testing in dependency injection systems often involves repetitive setup and brittle mock wiring. Suites is a unit testing framework that creates isolated test environments with a single declarative call, helping developers focus on test logic rather than infrastructure, boilerplate and setup.

When to use it​

Use Suites when you:

  • Need fast, maintainable unit tests for InversifyJS classes
  • Want compile-time type safety for all mocked dependencies
  • Need to eliminate repetitive mock setup boilerplate
  • Want to test classes with both isolation and selective collaboration
  • Need AI-assisted testing with clear, structured patterns
info

If you need full integration tests with real dependencies and actual InversifyJS containers, traditional container configuration may be more appropriate.

Core features​

  • Auto-generates type-safe mocks - All dependencies become fully typed Mocked<T> instances with compile-time validation
  • Solitary and sociable testing - Test classes in complete isolation or enable selective collaboration between business logic
  • InversifyJS-aware - Recognizes @injectable, @inject, @multiInject, @tagged, and @named decorators automatically
  • Virtual DI container - No actual InversifyJS container needed - analyzes classes and creates only required mocks
  • Token auto-mocking - Token-injected dependencies are mocked by default; use .mock(token).final(value) to supply a value or implementation
  • Framework integration - Works with Jest or Vitest

Installation​

For Suites 4 with InversifyJS 7.x or 8.x, use Node.js ^22.22.2 || ^24.15.0 || ^26.0.0.

For Jest, install matching jest and @jest/globals versions satisfying ^30.5.2. If your project stays on Jest 29, use a Suites release compatible with that runner version.

npm install -D @suites/unit @suites/di.inversify @suites/doubles.jest
info

Use @suites/doubles.vitest instead of @suites/doubles.jest for Vitest. The Sinon adapter has no Suites 4 release.

Add type references to a declaration file included by your test tsconfig:

/// <reference types="@suites/doubles.jest/unit" />
/// <reference types="@suites/di.inversify/metadata" />

For Vitest, use @suites/doubles.vitest/unit for the first reference. Keep one doubles reference and one DI metadata reference per test TypeScript project.

Enable experimentalDecorators and emitDecoratorMetadata, load reflect-metadata before decorated classes, and configure your test runner to emit legacy decorator metadata. See the Suites installation guide for runner setup and runtime requirements.

Quick Examples​

Solitary Testing​

Test a class in complete isolation with all dependencies automatically mocked:

order.service.ts
@injectable()
class OrderService {
constructor(
private readonly paymentGateway: PaymentGateway,
private readonly inventoryService: InventoryService
) {}

async processOrder(orderId: string) {
const inventory = await this.inventoryService.checkStock(orderId);
return this.paymentGateway.charge(inventory.total);
}
}
order.service.spec.ts
import { TestBed, type Mocked } from '@suites/unit';
import { injectable } from 'inversify';

describe('Order Service Unit Spec', () => {
let orderService: OrderService;
let paymentGateway: Mocked<PaymentGateway>;
let inventoryService: Mocked<InventoryService>;

beforeAll(async () => {
const { unit, unitRef } = await TestBed.solitary(OrderService).compile();

orderService = unit;
paymentGateway = unitRef.get(PaymentGateway);
inventoryService = unitRef.get(InventoryService);
});

it('should process order with payment', async () => {
inventoryService.checkStock.mockResolvedValue({ total: 100 });
paymentGateway.charge.mockResolvedValue({ success: true });

const result = await orderService.processOrder('123');

expect(result.success).toBe(true);
expect(paymentGateway.charge).toHaveBeenCalledWith(100);
});
});

Sociable Testing with Collaboration​

Test classes with their real collaborators while mocking external boundaries:

@injectable()
class PriceCalculator {
calculateTotal(items: Item[]): number {
return items.reduce((sum, item) => sum + item.price, 0);
}
}
@injectable()
class OrderService {
constructor(
private calculator: PriceCalculator,
private paymentGateway: PaymentGateway
) {}

async processOrder(items: Item[]) {
const total = this.calculator.calculateTotal(items);
return this.paymentGateway.charge(total);
}
}
import { TestBed, type Mocked } from '@suites/unit';

describe('Order Service Sociable Unit Spec', () => {
let orderService: OrderService;
let paymentGateway: Mocked<PaymentGateway>;

beforeAll(async () => {
const { unit, unitRef } = await TestBed.sociable(OrderService)
.expose(PriceCalculator) // Run the real calculator
.compile();

orderService = unit;
paymentGateway = unitRef.get(PaymentGateway);
});

it('should calculate and charge correct total', async () => {
paymentGateway.charge.mockResolvedValue({ success: true });

await orderService.processOrder([
{ price: 10 },
{ price: 20 }
]);

// Real calculator logic executed, only gateway is mocked
expect(paymentGateway.charge).toHaveBeenCalledWith(30);
});
});

.expose(PriceCalculator) selects that real collaborator; PaymentGateway remains mocked. Check exposed code for I/O. Suites does not automatically detect or prevent external calls.

Token Dependencies​

Token-injected dependencies are auto-mocked by default (read more about Dependency Inversion):

@injectable()
class PaymentService {
constructor(
private validator: PaymentValidator, // Class dependency
@inject('DATABASE') private database: Database, // Token dependency
) {}
}

describe('PaymentService', () => {
let service: PaymentService;
let validator: Mocked<PaymentValidator>;
let database: Mocked<Database>;

beforeAll(async () => {
const { unit, unitRef } = await TestBed.solitary(PaymentService).compile();

service = unit;
validator = unitRef.get(PaymentValidator); // By class
database = unitRef.get<Database>('DATABASE'); // By token
});
});

InversifyJS Metadata Support​

Suites fully supports InversifyJS metadata patterns for tagged and named bindings:

@injectable()
class Ninja {
constructor(
@inject('Weapon') @tagged('canThrow', true) private shuriken: Weapon
) {}
}

describe('Ninja', () => {
let ninja: Ninja;
let shuriken: Mocked<Weapon>;

beforeAll(async () => {
const { unit, unitRef } = await TestBed.solitary(Ninja).compile();

ninja = unit;

// Access mocks by tag metadata
shuriken = unitRef.get<Weapon>('Weapon', { canThrow: true });
});

it('should use correct weapons', () => {
// Retrieve the mock using the injection site's tag metadata
expect(shuriken).toBeDefined();
});
});

You can also configure mocks with metadata before compilation:

beforeAll(async () => {
const { unit, unitRef } = await TestBed.solitary(Ninja)
.mock<Weapon>('Weapon', { canThrow: true })
.impl(stub => ({
attack: stub().mockReturnValue('shuriken attack!')
}))
.compile();

ninja = unit;
shuriken = unitRef.get<Weapon>('Weapon', { canThrow: true });
});

How it works​

Suites uses a virtual DI container that analyzes Inversify's classes without initializing an actual container.

It:

  1. Detects @injectable and @inject decorators automatically
  2. Creates type-safe mocks for all dependencies
  3. Wires them together without framework overhead
  4. Uses mocks for unexposed dependencies unless you supply an implementation

This means you get the testing ergonomics of a DI framework without the initialization cost or complexity.

Documentation​

Full documentation with guides, API references, and examples: