|
1 |
| -<p align="center"> |
2 |
| - <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a> |
3 |
| -</p> |
4 |
| - |
5 |
| -[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 |
6 |
| -[circleci-url]: https://circleci.com/gh/nestjs/nest |
7 |
| - |
8 |
| - <p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p> |
9 |
| - <p align="center"> |
10 |
| -<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a> |
11 |
| -<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a> |
12 |
| -<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a> |
13 |
| -<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a> |
14 |
| -<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a> |
15 |
| -<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a> |
16 |
| -<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a> |
17 |
| -<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a> |
18 |
| - <a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a> |
19 |
| - <a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a> |
20 |
| - <a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a> |
21 |
| -</p> |
22 |
| - <!--[](https://opencollective.com/nest#backer) |
23 |
| - [](https://opencollective.com/nest#sponsor)--> |
24 |
| - |
25 |
| -## Description |
26 |
| - |
27 |
| -[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. |
28 |
| - |
29 |
| -## Installation |
| 1 | +# Unit testing NestJS with Typeorm in memory |
| 2 | + |
| 3 | +Assuming you already configured TypeORM in your NestJS project. |
| 4 | +If you need to setup TypeORM, consider reading the [awesome NestJS documentation](https://docs.nestjs.com/techniques/database). |
| 5 | + |
| 6 | +For the sake of testing something, I'll setup a simple API that will serve spaceships! You can access the [sample repository on github](https://github.com/Webeleon/unit-testing-nestjs-using-typeorm-in-memory). |
| 7 | + |
| 8 | +## Install SQLITE |
30 | 9 |
|
31 | 10 | ```bash
|
32 |
| -$ npm install |
| 11 | +npm i -D better-sqlite3 |
33 | 12 | ```
|
34 | 13 |
|
35 |
| -## Running the app |
| 14 | +## A few helpers |
36 | 15 |
|
37 |
| -```bash |
38 |
| -# development |
39 |
| -$ npm run start |
| 16 | +Ok, we can write this for every test files BUT we are lazy developers and will not spend our time re-writing the same code over and over and over... |
40 | 17 |
|
41 |
| -# watch mode |
42 |
| -$ npm run start:dev |
| 18 | +Let's write a simple helper function that will provide an easy to import pre-configured TypeORM module! |
| 19 | +`src/test-utils/TypeORMSQLITETestingModule.ts` |
43 | 20 |
|
44 |
| -# production mode |
45 |
| -$ npm run start:prod |
| 21 | +```ts |
| 22 | +import { TypeOrmModule } from '@nestjs/typeorm'; |
| 23 | +import { Spaceship } from '../spaceships/spaceship.entity'; |
| 24 | + |
| 25 | +export const TypeOrmSQLITETestingModule = () => [ |
| 26 | + TypeOrmModule.forRoot({ |
| 27 | + type: 'better-sqlite3', |
| 28 | + database: ':memory:', |
| 29 | + dropSchema: true, |
| 30 | + entities: [Spaceship], |
| 31 | + synchronize: true, |
| 32 | + }), |
| 33 | + TypeOrmModule.forFeature([Spaceship]), |
| 34 | +]; |
46 | 35 | ```
|
47 | 36 |
|
48 |
| -## Test |
| 37 | +This one is not mandatory but a simple test dataset is always a nice to have. `src/test-utils/testDataset.seed.ts` |
| 38 | + |
| 39 | +```ts |
| 40 | +import { getConnection } from 'typeorm'; |
| 41 | +import { Spaceship } from '../spaceships/spaceship.entity'; |
| 42 | + |
| 43 | +export const testDatasetSeed = async () => { |
| 44 | + const connection = await getConnection(); |
| 45 | + const entityManager = connection.createEntityManager(); |
| 46 | + |
| 47 | + entityManager.insert<Spaceship>(Spaceship, { |
| 48 | + name: 'moa', |
| 49 | + type: 'cruiser', |
| 50 | + origin: 'caldari', |
| 51 | + }); |
| 52 | + entityManager.insert<Spaceship>(Spaceship, { |
| 53 | + name: 'caracal', |
| 54 | + type: 'cruiser', |
| 55 | + origin: 'caldari', |
| 56 | + }); |
| 57 | + entityManager.insert<Spaceship>(Spaceship, { |
| 58 | + name: 'rokh', |
| 59 | + type: 'battleship', |
| 60 | + origin: 'caldari', |
| 61 | + }); |
| 62 | +}; |
| 63 | +``` |
49 | 64 |
|
50 |
| -```bash |
51 |
| -# unit tests |
52 |
| -$ npm run test |
| 65 | +## Using the helpers in a test file |
53 | 66 |
|
54 |
| -# e2e tests |
55 |
| -$ npm run test:e2e |
| 67 | +In order to allow tests to use the im memory database, you'll just need to call the function `...TypeOrmSQLITETestingModule()` and spread it since it provide the `TypeOrmModule.forRoot` and `TypeOrmModule.forFeature`. |
56 | 68 |
|
57 |
| -# test coverage |
58 |
| -$ npm run test:cov |
| 69 | +Finally, seed the test data (or not) `await testDatasetSeed();`. |
| 70 | + |
| 71 | +```ts |
| 72 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 73 | +import { SpaceshipsService } from './spaceships.service'; |
| 74 | +import { TypeOrmSQLITETestingModule } from '../test-utils/TypeOrmSQLITETestingModule'; |
| 75 | +import { testDatasetSeed } from '../test-utils/testDataset.seed'; |
| 76 | + |
| 77 | +describe('SpaceshipsService', () => { |
| 78 | + let service: SpaceshipsService; |
| 79 | + |
| 80 | + beforeEach(async () => { |
| 81 | + const module: TestingModule = await Test.createTestingModule({ |
| 82 | + imports: [...TypeOrmSQLITETestingModule()], |
| 83 | + providers: [SpaceshipsService], |
| 84 | + }).compile(); |
| 85 | + |
| 86 | + service = module.get<SpaceshipsService>(SpaceshipsService); |
| 87 | + await testDatasetSeed(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('listSpaceships', async () => { |
| 91 | + const spaceships = await service.listSpaceships(); |
| 92 | + expect(spaceships).toHaveLength(3); |
| 93 | + }); |
| 94 | +}); |
59 | 95 | ```
|
60 | 96 |
|
61 |
| -## Support |
| 97 | +## Questions? |
| 98 | + |
| 99 | + |
62 | 100 |
|
63 |
| -Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). |
| 101 | +I'll be glad to answers questions in the comments. |
64 | 102 |
|
65 |
| -## Stay in touch |
| 103 | +If you liked my discord consider joining my coding lair! |
| 104 | +:phone:[Webeleon coding lair on discord](https://discord.gg/h7HzYzD82p) |
66 | 105 |
|
67 |
| -- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) |
68 |
| -- Website - [https://nestjs.com](https://nestjs.com/) |
69 |
| -- Twitter - [@nestframework](https://twitter.com/nestframework) |
| 106 | +You can also email me and offer me a contract :moneybag: |
| 107 | +:envelope:[Email me! ]([email protected]) |
70 | 108 |
|
71 |
| -## License |
| 109 | +And since I'm a nice guy, here, take this sample repo containing a working codebase! |
| 110 | +:gift:[Get the code of the tuto from github](https://github.com/Webeleon/unit-testing-nestjs-using-typeorm-in-memory) |
72 | 111 |
|
73 |
| -Nest is [MIT licensed](LICENSE). |
| 112 | +<script type="text/javascript" src="https://cdnjs.buymeacoffee.com/1.0.0/button.prod.min.js" data-name="bmc-button" data-slug="webeleon" data-color="#FFDD00" data-emoji="" data-font="Cookie" data-text="Buy me a coffee" data-outline-color="#000000" data-font-color="#000000" data-coffee-color="#ffffff" ></script> |
0 commit comments