Quick Start

Write your first unit test with NestJS, InversifyJS, or InjectionJS. Choose your framework below for a complete example with application code, test setup, and assertions. Every path supports Jest and Vitest.

Choose your framework

Follow the guide for the framework your project already uses. You only need one path.

Need to configure your project first? Follow Installation for the adapter, type declarations, and runner configuration. Each framework guide points to the settings it needs.

Follow the same testing flow

Each guide starts with a solitary test, a unit test where all dependencies are mocked. Suites creates the unit, the class under test, and its dependency mocks.

  1. Define the service and its dependencies. Use your framework’s decorators and share any injection tokens between the service and its test.
  2. Create the test bed in beforeAll. Call TestBed.solitary(UserService).compile(). The result provides unit and unitRef.
  3. Retrieve the dependency mocks. Use unitRef.get(Dependency) for class dependencies or unitRef.get<Dependency>(TOKEN) for tokens.
  4. Set behavior and assert the result in it. Configure the mock’s response, call the unit, and check its output and dependency calls.

The framework determines how Suites discovers dependencies. The TestBed workflow stays the same. Each guide includes the full imports and application code for its example.

Run your first test

After copying the application and test code from your chosen guide, run your configured runner:

Terminal window
# Jest
pnpm exec jest
# Vitest
pnpm exec vitest run

The first example in each guide should produce one passing test. The service receives the configured mock response without starting an application or connecting to a database.

The NestJS and InversifyJS guides also include a sociable test, a unit test where selected dependencies are real. Add that example to exercise a real collaborator with TestBed.sociable(...).expose(...); both tests should then pass. InjectionJS supports the same API for class dependencies that use constructor injection.

Summary

  • Choose one framework guide and either Jest or Vitest.
  • Compile the test bed in beforeAll, then configure behavior and make assertions in it.
  • Use unit to call your class and unitRef to access its dependency mocks.

Start your first test

Choose your framework to copy its complete example:

Next steps

After your framework’s first test passes, continue with the testing pattern you need: