Skip to content

Commit 67d590e

Browse files
committed
fix: address PR comments
1 parent c8b7861 commit 67d590e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

content/fundamentals/unit-testing.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,36 @@ Instead of using the production version of any provider, you can override it wit
162162

163163
#### Auto mocking
164164

165-
Nest also allows you to define a mock factory to apply to all of your missing dependencies. This is useful for cases where you have a large number of dependencies in a class and mocking all of them will take a long time and a lot of setup. To make use of this feature, the `createTestingModule()` will need to be chained up with the `useMocker()` method, passing a factory for your dependency mocks. This factory can take in an optional token, which is an instance token, any token which is valid for a Nest provider, and returns a mock implementation. Otherwise, a general mock factory, like `createMock` from [`@golevelup/ts-jest`](https://github.com/golevelup/nestjs/tree/master/packages/testing) can be passed directly.
165+
Nest also allows you to define a mock factory to apply to all of your missing dependencies. This is useful for cases where you have a large number of dependencies in a class and mocking all of them will take a long time and a lot of setup. To make use of this feature, the `createTestingModule()` will need to be chained up with the `useMocker()` method, passing a factory for your dependency mocks. This factory can take in an optional token, which is an instance token, any token which is valid for a Nest provider, and returns a mock implementation. The below is an example of creating a generic mocker using [`jest-mock`](https://www.npmjs.com/package/jest-mock) and a specific mock for `CatsService` using `jest.fn()`.
166166

167167
```typescript
168+
const moduleMocker = new ModuleMocker(global);
169+
168170
describe('CatsController', () => {
169171
let controller: CatsController;
170172

171173
beforeEach(async () => {
172-
const modRef = await Test.createTestingModule({
174+
const moduleRef = await Test.createTestingModule({
173175
controllers: [CatsController],
174176
})
175177
.useMocker((token) => {
176178
if (token === CatsService) {
177179
return { findAll: jest.fn().mockResolveValue(results) }
178180
}
181+
if (typeof token === 'function') {
182+
const mockMetadata = moduleMocker.getMetadata(token) as MockFunctionMetadata<any, any>
183+
const Mock = moduleMocker.generateFromMetadata(mockMetadata)
184+
return new Mock()
179185
})
180186
.compile();
181187
controller = modRef.get(CatsController);
182188
});
183189
})
184190
```
185191
186-
You can also retrieve these mocks out of the testing container as you normally would custom providers, `modRef.get(CatsService)`.
192+
> info **Hint** A general mock factory, like `createMock` from [`@golevelup/ts-jest`](https://github.com/golevelup/nestjs/tree/master/packages/testing) can also be passed directly.
193+
194+
You can also retrieve these mocks out of the testing container as you normally would custom providers, `moduleRef.get(CatsService)`.
187195
188196
#### End-to-end testing
189197

0 commit comments

Comments
 (0)