createNestApplication()
|
Creates and returns a Nest application (INestApplication instance) based on the given module.
Note that you must manually initialize the application using the init() method.
|
createNestMicroservice()
|
Creates and returns a Nest microservice (INestMicroservice instance) based on the given module.
|
get()
|
Retrieves a static instance of a controller or provider (including guards, filters, etc.) available in the application context. Works like the same method of the module reference class.
|
resolve()
|
Retrieves a dynamically created scoped instance (request or transient) of a controller or provider (including guards, filters, etc.) available in the application context. Works like the same method of the module reference class.
|
select()
|
Navigates through the module's dependency graph; can be used to retrieve a specific instance from the selected module (used along with strict mode (strict: true) in the get() method).
|
> info **Hint** Keep your e2e test files inside the `test` directory. Test files should have an `.e2e-spec` suffix.
#### Overriding globally registered enhancers
If you have a globally registered guard (or pipe, interceptor, or filter), you need to take a few more steps to override that enhancer. To recap, the original registration looks like this:
```typescript
providers: [
{
provide: APP_GUARD,
useClass: JwtAuthGuard,
},
],
```
This registers the guard as a "multi" provider through the `APP_*` token. To be able to replace `JwtAuthGuard` here, the registration needs to use an existing provider in this slot:
```typescript
providers: [
{
provide: APP_GUARD,
useExisting: JwtAuthGuard,
// ^^^^^^^^ notice the use of 'useExisting' instead of 'useClass'
},
JwtAuthGuard,
],
```
> info **Hint** Change `useClass` to `useExisting` to reference a registered provider instead of having Nest instantiate it behind the token.
`JwtAuthGuard` is now visible to Nest as a regular provider that can be overridden when creating the `TestingModule`:
```typescript
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(JwtAuthGuard)
.useClass(MockAuthGuard)
.compile();
```
All your tests now use `MockAuthGuard` on every request.
#### Testing request-scoped instances
[Request-scoped](/fundamentals/injection-scopes) providers are created uniquely for each incoming **request**, and the instance is garbage-collected after the request has been processed. This poses a problem, because you can't access a dependency injection sub-tree generated specifically for a tested request.
As shown in the sections above, the `resolve()` method retrieves a dynamically instantiated class. Also, as described in [resolving scoped providers](/fundamentals/module-ref#resolving-scoped-providers), you can pass a unique context identifier to control the lifecycle of a DI container sub-tree. You can combine the two in a testing context.
The strategy is to generate a context identifier beforehand and force Nest to use this particular ID to create a sub-tree for all incoming requests. This way, you can retrieve the instances created for a tested request.
To accomplish this, use `vi.spyOn()` on the `ContextIdFactory`:
```typescript
const contextId = ContextIdFactory.create();
vi
.spyOn(ContextIdFactory, 'getByRequest')
.mockImplementation(() => contextId);
```
You can now use the `contextId` to access a single generated DI container sub-tree for any subsequent request.
```typescript
catsService = await moduleRef.resolve(CatsService, contextId);
```