|
| 1 | +<h1 align="center">Welcome to nest-spectator 👋</h1> |
| 2 | +<p> |
| 3 | + <a href="https://www.npmjs.com/package/nest-spectator" target="_blank"> |
| 4 | + <img alt="Version" src="https://img.shields.io/npm/v/nest-spectator.svg"> |
| 5 | + </a> |
| 6 | + <a href="#" target="_blank"> |
| 7 | + <img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" /> |
| 8 | + </a> |
| 9 | +</p> |
| 10 | + |
| 11 | +> Auto-mocking for Nestjs providers |
| 12 | +
|
| 13 | +### 🏠 [Homepage](https://www.npmjs.com/package/nest-spectator) |
| 14 | + |
| 15 | +## Author |
| 16 | + |
| 17 | +👤 **Jay Bell <[email protected]>** |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +See `packages/nest-spectator/__tests__` for reference: |
| 22 | + |
| 23 | +Let's assume you we have the following 2 different Nestjs services: |
| 24 | + |
| 25 | +``` |
| 26 | +@Injectable() |
| 27 | +class PrimaryService { |
| 28 | + constructor(secondaryService: SecondaryService) { |
| 29 | + } |
| 30 | +
|
| 31 | + testFunction(): string { |
| 32 | + return 'test'; |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +and |
| 38 | + |
| 39 | +``` |
| 40 | +@Injectable() |
| 41 | +class SecondaryService { |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +and this controller: |
| 46 | + |
| 47 | +``` |
| 48 | +@Controller() |
| 49 | +class PrimaryController { |
| 50 | + constructor(primaryService: PrimaryService) { |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Currently we have something like: |
| 56 | + |
| 57 | +``` |
| 58 | +import { Test } from '@nestjs/testing'; |
| 59 | +import { PrimaryController } from './primary.controller'; |
| 60 | +import { PrimaryService } from './primary.service'; |
| 61 | +import { SecondaryService } from './secondary.service'; |
| 62 | +
|
| 63 | +describe('PrimaryController', () => { |
| 64 | + let primaryController: PrimaryController; |
| 65 | + let primaryService: PrimaryService; |
| 66 | +
|
| 67 | + beforeEach(async () => { |
| 68 | + const module = await Test.createTestingModule({ |
| 69 | + controllers: [PrimaryController], |
| 70 | + providers: [PrimaryService, SecondaryService], |
| 71 | + }).compile(); |
| 72 | +
|
| 73 | + primaryService = module.get<PrimaryService>(PrimaryService); |
| 74 | + primaryController = module.get<PrimaryController>(PrimaryController); |
| 75 | + }); |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +Which is fine for small projects but as your code base grows you could have many injections in your classes constructor that you are testing. |
| 80 | +Assuming you plan on creating mocks for the services injected into `PrimaryController` classes or you want to spy on the classes methods of `PrimaryService` your code can start to grow more and more for each time you have a new spec file. |
| 81 | + |
| 82 | +If you do not mock our your services and instead just want to spy on them, your spec files will grow very large because each service you provider in the `Test.createTestingModule` will need to have all of it's services injected as well. |
| 83 | + |
| 84 | +You can see the effect of this in the sample above, `SecondaryService` is injected because `PrimaryService` injects it which in turn is injected into `PrimaryController` |
| 85 | + |
| 86 | +If you are wanting to create mocks for each of these services injected in your class being tested then it would look something like this: |
| 87 | + |
| 88 | +``` |
| 89 | +import { Test } from '@nestjs/testing'; |
| 90 | +import { PrimaryController } from './primary.controller'; |
| 91 | +import { PrimaryService } from './primary.service'; |
| 92 | +import { SecondaryService } from './secondary.service'; |
| 93 | +
|
| 94 | +const mockPrimaryService = { |
| 95 | + testFunction: () => {} |
| 96 | +} |
| 97 | +
|
| 98 | +class MockPrimaryService { |
| 99 | + testFunction(): void { |
| 100 | + } |
| 101 | +} |
| 102 | +
|
| 103 | +describe('PrimaryController', () => { |
| 104 | + let primaryController: PrimaryController; |
| 105 | + let primaryService: PrimaryService; |
| 106 | +
|
| 107 | + beforeEach(async () => { |
| 108 | + const module = await Test.createTestingModule({ |
| 109 | + controllers: [PrimaryController], |
| 110 | + providers: [ |
| 111 | + {provide: PrimaryService, useValue: mockPrimaryService} |
| 112 | + // OR |
| 113 | + {provide: PrimaryService, useClass: MockPrimaryService} |
| 114 | + ], |
| 115 | + }).compile(); |
| 116 | +
|
| 117 | + primaryService = module.get<PrimaryService>(PrimaryService); |
| 118 | + primaryController = module.get<PrimaryController>(PrimaryController); |
| 119 | + }); |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +Now you will have to maintain these mock objects for each of your services and ensure you provide over your implementation services in each of your spec files. |
| 124 | + |
| 125 | +This is where `nest-spectator` comes in, inspired by [@ngneat/spectator](https://github.com/ngneat/spectator) for Angular, `nest-spectator` creates a layer on top of the `@nestjs/testing` |
| 126 | +`Test.creatingTestingModule` to provide the functionality to auto mock your services so that your module instantiation in tests turns into: |
| 127 | + |
| 128 | +``` |
| 129 | +beforeEach(async () => { |
| 130 | + module = await createTestingModuleFactory( |
| 131 | + { |
| 132 | + imports: [], |
| 133 | + controllers: [PrimaryController], |
| 134 | + providers: [PrimaryService], |
| 135 | + mocks: [PrimaryService] |
| 136 | + }, |
| 137 | + ).compile(); |
| 138 | +}); |
| 139 | +``` |
| 140 | + |
| 141 | +The `createTestingModuleFactory` accepts all the same values as the `Test.createTestingModule` function except that there is an option property `mocks?: Array<Type<any>>` |
| 142 | +that will accept the providers you want to auto mock. This function will return the same value (`TestingModuleBuilder`) as `Test.createTestingModule` will which means we just call |
| 143 | +`.compile()` on it after to get our testing module. |
| 144 | + |
| 145 | +If we need access to our services provided to this testing module we get them the same way as we would before since we just have a `TestingModule`. |
| 146 | + |
| 147 | +``` |
| 148 | +const primaryService = module.get<PrimaryService>(PrimaryService); |
| 149 | +``` |
| 150 | + |
| 151 | +If `PrimaryService` was included in the `mocks` array during test module instantiation than it will be an object that mirrors the structure of your class as if it was provided normally except |
| 152 | +that the methods, getters and setters will all be `jest` Spys themselves. |
| 153 | + |
| 154 | +When providing `PrimaryService` in the mocks array and using `module.get<T>(T)` to get the instance of the provider it will return `SpyObject<PrimaryService>` instead of just `PrimaryService`. |
| 155 | + |
| 156 | +This package is still in alpha so there way be unintended side effects or gaps in the logic. If there is anything you would like to see include please feel free to open an issue. |
| 157 | + |
| 158 | +## 🤝 Contributing |
| 159 | + |
| 160 | +Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](https://github.com/yharaskrik/nest-testing/issues). You can also take a look at the [contributing guide](https://www.npmjs.com/package/nest-spectator/blob/master/CONTRIBUTING.md). |
| 161 | + |
| 162 | +## Show your support |
| 163 | + |
| 164 | +Give a ⭐️ if this project helped you! |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_ |
0 commit comments