Testing
Nothing in a service refers to a container, so a test decides what the service is wired to. This page goes from the plain approach, which covers most tests, to the two helper containers in @navios/di/testing.
The examples use Vitest, which does not compile standard decorators by itself. Testing in the Server docs has the configuration; bun test needs none.
A fresh container per test
Instances live in a container, not in the classes. A new Container starts with none, so tests cannot leak state into each other.
dispose() in afterEach runs onServiceDestroy hooks, so connections and timers opened by one test are closed before the next.
Replacing a dependency
Give the container a provider for the thing to replace. Everything else resolves as in production.
The replacement can be a value, a class or a function, and has to have the public members of what it replaces. A class is useful when the fake needs dependencies or lifecycle hooks of its own:
To add behaviour to the real implementation instead of replacing it, for example to count calls, use container.decorate().
Scopes and lifecycle
Request-scoped services are tested through a scope, exactly as they are used:
Lifecycle hooks are ordinary methods, so a spy on the instance is enough:
Failures are DIErrors. Assert on code rather than on the message:
TestContainer
TestContainer is a Container with a fluent way to bind replacements and a few assertions about the container itself.
How bind() differs from container.provide():
- It may be called after the token was resolved. The existing instance and everything that depends on it are destroyed, and the next resolution uses the new binding. For an alias that is everything that depends on what it pointed to, which stays itself.
toValue()of a singleton stores the value at once, so the token counts as resolved. A value for a request-scoped token is served in scopes only, as in production.- Instances that an open scope already holds stay as they are.
clear()destroys all instances and keeps the bindings and the container. Call it inafterEachwhen one container is shared by a wholedescribeblock. A container that was disposed withdispose()is closed for good.
The dependency graph of what has been resolved can be snapshotted, which catches accidental new dependencies:
The API reference lists every assertion. The scope assertions (expectSingleton and friends) check the scope of the provider in use. The call and lifecycle assertions (expectCalled, expectInitialized and friends) only see what a test or a fake reported through recordMethodCall() and recordLifecycleEvent(); the container does not observe real instances.
UnitTestContainer
UnitTestContainer is for testing one class in isolation. It is a strict container: only what is listed in providers can be resolved, so a test cannot reach a real database because someone added an inject().
A provider entry has a token and at most one of useValue, useClass and useFactory, which is called with the factory context and the arguments. With none of them, a class is registered as itself. What is given is checked against the public members of the token's type, except that any string, number or boolean passes for a literal type: TypeScript infers the list before it looks at the tokens.
Instances returned by get() and getSync(), of the container and of its scopes, are wrapped in a proxy that records every method call, which is what the expect… methods read. Only calls made through that returned object are recorded. Calls one service makes on another go to the real injected instance, and calls a service makes to its own methods stay inside it, so assert on those with a spy inside the useValue fake. Spies keep working through the proxy.
A dependency that is not listed fails with ProviderNotFound when the class under test is built. With allowUnregistered: true an unlisted token resolves to a placeholder instead, whether it is requested from the container or injected into the class under test. The placeholder throws when any property is used, so a test fails only if the code actually touches a dependency it did not list.