Skip to content

Commit eb78ef9

Browse files
docs(testing): mention http adapter limitation #2823
1 parent a9cbb00 commit eb78ef9

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

content/fundamentals/unit-testing.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ describe('CatsController', () => {
183183
return { findAll: jest.fn().mockResolvedValue(results) };
184184
}
185185
if (typeof token === 'function') {
186-
const mockMetadata = moduleMocker.getMetadata(token) as MockFunctionMetadata<any, any>;
186+
const mockMetadata = moduleMocker.getMetadata(
187+
token,
188+
) as MockFunctionMetadata<any, any>;
187189
const Mock = moduleMocker.generateFromMetadata(mockMetadata);
188190
return new Mock();
189191
}
@@ -286,7 +288,9 @@ describe('Cats', () => {
286288
> let app: NestFastifyApplication;
287289
>
288290
> beforeAll(async () => {
289-
> app = moduleRef.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
291+
> app = moduleRef.createNestApplication<NestFastifyApplication>(
292+
> new FastifyAdapter(),
293+
> );
290294
>
291295
> await app.init();
292296
> await app.getHttpAdapter().getInstance().ready();
@@ -309,7 +313,13 @@ describe('Cats', () => {
309313
> });
310314
> ```
311315
312-
In this example, we build on some of the concepts described earlier. In addition to the `compile()` method we used earlier, we now use the `createNestApplication()` method to instantiate a full Nest runtime environment. We save a reference to the running app in our `app` variable so we can use it to simulate HTTP requests.
316+
In this example, we build on some of the concepts described earlier. In addition to the `compile()` method we used earlier, we now use the `createNestApplication()` method to instantiate a full Nest runtime environment.
317+
318+
One caveat to consider is that when your application is compiled using the `compile()` method, the `HttpAdapterHost#httpAdapter` will be undefined at that time. This is because there isn't an HTTP adapter or server created yet during this compilation phase. If your test requires the `httpAdapter`, you should use the `createNestApplication()` method to create the application instance, or refactor your project to avoid this dependency when initializing the dependencies graph.
319+
320+
Alright, let's break down the example:
321+
322+
We save a reference to the running app in our `app` variable so we can use it to simulate HTTP requests.
313323
314324
We simulate HTTP tests using the `request()` function from Supertest. We want these HTTP requests to route to our running Nest app, so we pass the `request()` function a reference to the HTTP listener that underlies Nest (which, in turn, may be provided by the Express platform). Hence the construction `request(app.getHttpServer())`. The call to `request()` hands us a wrapped HTTP Server, now connected to the Nest app, which exposes methods to simulate an actual HTTP request. For example, using `request(...).get('/cats')` will initiate a request to the Nest app that is identical to an **actual** HTTP request like `get '/cats'` coming in over the network.
315325
@@ -437,7 +447,9 @@ To accomplish this, use `jest.spyOn()` on the `ContextIdFactory`:
437447

438448
```typescript
439449
const contextId = ContextIdFactory.create();
440-
jest.spyOn(ContextIdFactory, 'getByRequest').mockImplementation(() => contextId);
450+
jest
451+
.spyOn(ContextIdFactory, 'getByRequest')
452+
.mockImplementation(() => contextId);
441453
```
442454

443455
Now we can use the `contextId` to access a single generated DI container sub-tree for any subsequent request.

0 commit comments

Comments
 (0)