From a33e2fe9063aa96f3cc647a63fcb6ecf02c6b9b2 Mon Sep 17 00:00:00 2001 From: "szymon.habrainski" Date: Wed, 25 Oct 2023 12:33:07 +0200 Subject: [PATCH] docs(unit-testing): explain `overrideMiddleware` --- content/fundamentals/unit-testing.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/content/fundamentals/unit-testing.md b/content/fundamentals/unit-testing.md index f64fafbfb8..e8c8ce3aa2 100644 --- a/content/fundamentals/unit-testing.md +++ b/content/fundamentals/unit-testing.md @@ -313,9 +313,9 @@ In this example, we build on some of the concepts described earlier. In addition 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. -In this example, we also provide an alternate (test-double) implementation of the `CatsService` which simply returns a hard-coded value that we can test for. Use `overrideProvider()` to provide such an alternate implementation. Similarly, Nest provides methods to override modules, guards, interceptors, filters and pipes with the `overrideModule()`, `overrideGuard()`, `overrideInterceptor()`, `overrideFilter()`, and `overridePipe()` methods respectively. +In this example, we also provide an alternate (test-double) implementation of the `CatsService` which simply returns a hard-coded value that we can test for. Use `overrideProvider()` to provide such an alternate implementation. Similarly, Nest provides methods to override modules, middleware, guards, interceptors, filters and pipes with the `overrideModule()`, `overrideMiddleware()`, `overrideGuard()`, `overrideInterceptor()`, `overrideFilter()`, and `overridePipe()` methods respectively. -Each of the override methods (except for `overrideModule()`) returns an object with 3 different methods that mirror those described for [custom providers](https://docs.nestjs.com/fundamentals/custom-providers): +Except for `overrideModule()` and `overrideMiddleware()`, each of the override methods returns an object with 3 different methods that mirror those described for [custom providers](https://docs.nestjs.com/fundamentals/custom-providers): - `useClass`: you supply a class that will be instantiated to provide the instance to override the object (provider, guard, etc.). - `useValue`: you supply an instance that will override the object. @@ -332,6 +332,17 @@ const moduleRef = await Test.createTestingModule({ .compile(); ``` +Similarly, `overrideMiddleware()` returns an object that has a `use()` method which receives middleware that should override the original middleware. In the following example, we replace `CatsMiddleware` with `AlternateMiddlewareCalledFirst` and `AlternateMiddlewareCalledSecond` in the middleware stack: + +```typescript +const moduleRef = await Test.createTestingModule({ + imports: [AppModule], +}) + .overrideMiddleware(CatsMiddleware) + .use(AlternateMiddlewareCalledFirst, AlternateMiddlewareCalledSecond) + .compile(); +``` + Each of the override method types, in turn, returns the `TestingModule` instance, and can thus be chained with other methods in the [fluent style](https://en.wikipedia.org/wiki/Fluent_interface). You should use `compile()` at the end of such a chain to cause Nest to instantiate and initialize the module. Also, sometimes you may want to provide a custom logger e.g. when the tests are run (for example, on a CI server). Use the `setLogger()` method and pass an object that fulfills the `LoggerService` interface to instruct the `TestModuleBuilder` how to log during tests (by default, only "error" logs will be logged to the console).