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.
- Define the service and its dependencies. Use your framework’s decorators and share any injection tokens between the service and its test.
- Create the test bed in
beforeAll. CallTestBed.solitary(UserService).compile(). The result providesunitandunitRef. - Retrieve the dependency mocks. Use
unitRef.get(Dependency)for class dependencies orunitRef.get<Dependency>(TOKEN)for tokens. - 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:
# Jestpnpm exec jest
# Vitestpnpm exec vitest runThe 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 init. - Use
unitto call your class andunitRefto 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:
- Solitary Unit Tests: Configure mocks and test a class in isolation.
- Sociable Unit Tests: Keep selected collaborators real while mocking external dependencies.
- How Suites works: Understand how Suites discovers dependencies and constructs your class.