From 9df936789aa022ff6e36bd0427f747b542eac559 Mon Sep 17 00:00:00 2001 From: Kim Kern Date: Sat, 12 Oct 2019 22:31:37 +0200 Subject: [PATCH 1/3] doc(samples) add unit tests for 13-mongo-typeorm see #1539 --- sample/13-mongo-typeorm/package-lock.json | 15 +++++++++ sample/13-mongo-typeorm/package.json | 13 ++++++++ .../src/photo/photo.controller.spec.ts | 31 ++++++++++++++++++ .../src/photo/photo.service.spec.ts | 32 +++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts create mode 100644 sample/13-mongo-typeorm/src/photo/photo.service.spec.ts diff --git a/sample/13-mongo-typeorm/package-lock.json b/sample/13-mongo-typeorm/package-lock.json index bb79d06523e..ae8cd9fedb6 100644 --- a/sample/13-mongo-typeorm/package-lock.json +++ b/sample/13-mongo-typeorm/package-lock.json @@ -1103,6 +1103,21 @@ "@types/istanbul-lib-report": "*" } }, + "@types/jest": { + "version": "24.0.18", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-24.0.18.tgz", + "integrity": "sha512-jcDDXdjTcrQzdN06+TSVsPPqxvsZA/5QkYfIZlq1JMw7FdP5AZylbOc+6B/cuDurctRe+MziUMtQ3xQdrbjqyQ==", + "dev": true, + "requires": { + "@types/jest-diff": "*" + } + }, + "@types/jest-diff": { + "version": "20.0.1", + "resolved": "https://registry.npmjs.org/@types/jest-diff/-/jest-diff-20.0.1.tgz", + "integrity": "sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==", + "dev": true + }, "@types/json5": { "version": "0.0.29", "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", diff --git a/sample/13-mongo-typeorm/package.json b/sample/13-mongo-typeorm/package.json index 931903d12e8..2d30180433b 100644 --- a/sample/13-mongo-typeorm/package.json +++ b/sample/13-mongo-typeorm/package.json @@ -34,6 +34,7 @@ "@nestjs/schematics": "6.7.0", "@nestjs/testing": "6.8.2", "@types/express": "4.17.1", + "@types/jest": "^24.0.18", "@types/node": "12.7.11", "@types/supertest": "2.0.8", "jest": "24.9.0", @@ -45,5 +46,17 @@ "tsconfig-paths": "3.9.0", "tslint": "5.20.0", "typescript": "3.6.4" + }, + "jest": { + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": "src", + "testRegex": ".spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + } } } diff --git a/sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts b/sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts new file mode 100644 index 00000000000..d5561346769 --- /dev/null +++ b/sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts @@ -0,0 +1,31 @@ +import { Test } from '@nestjs/testing'; +import { PhotoController } from './photo.controller'; +import { PhotoService } from './photo.service'; + +describe('Photo Controller', () => { + let controller: PhotoController; + let service; + + beforeAll(async () => { + const module = await Test.createTestingModule({ + controllers: [PhotoController], + providers: [ + { + provide: PhotoService, + useFactory: jest.fn(() => ({ + findAll: jest.fn(), + })), + }, + ], + }).compile(); + controller = module.get(PhotoController); + service = module.get(PhotoService); + }); + + it('should return all photos from the service', async () => { + service.findAll.mockReturnValueOnce([]); + expect(await controller.findAll()).toEqual([]); + expect(service.findAll).toHaveBeenCalledTimes(1); + }); + +}); diff --git a/sample/13-mongo-typeorm/src/photo/photo.service.spec.ts b/sample/13-mongo-typeorm/src/photo/photo.service.spec.ts new file mode 100644 index 00000000000..814f2bdb63d --- /dev/null +++ b/sample/13-mongo-typeorm/src/photo/photo.service.spec.ts @@ -0,0 +1,32 @@ +import { Test } from '@nestjs/testing'; +import { PhotoService } from './photo.service'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { Photo } from './photo.entity'; + +describe('Photo Service', () => { + let service: PhotoService; + let repository; + + beforeAll(async () => { + const module = await Test.createTestingModule({ + controllers: [PhotoService], + providers: [ + { + provide: getRepositoryToken(Photo), + useFactory: jest.fn(() => ({ + find: jest.fn(), + })), + }, + ], + }).compile(); + service = module.get(PhotoService); + repository = module.get(getRepositoryToken(Photo)); + }); + + it('should return all photos from the repository', async () => { + repository.find.mockReturnValueOnce([]); + expect(await service.findAll()).toEqual([]); + expect(repository.find).toHaveBeenCalledTimes(1); + }); + +}); From 4605515a606ce62546cf2a30f0e4bd9e409a34be Mon Sep 17 00:00:00 2001 From: Kim Kern Date: Sat, 12 Oct 2019 22:31:56 +0200 Subject: [PATCH 2/3] doc(samples) add e2e test for 13-mongo-typeorm see #1539 --- sample/13-mongo-typeorm/.gitignore | 3 +- sample/13-mongo-typeorm/test/jest-e2e.json | 13 ++++++++ .../13-mongo-typeorm/test/photos.e2e-spec.ts | 30 +++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 sample/13-mongo-typeorm/test/jest-e2e.json create mode 100644 sample/13-mongo-typeorm/test/photos.e2e-spec.ts diff --git a/sample/13-mongo-typeorm/.gitignore b/sample/13-mongo-typeorm/.gitignore index b5e5f97553a..7f935e75080 100644 --- a/sample/13-mongo-typeorm/.gitignore +++ b/sample/13-mongo-typeorm/.gitignore @@ -13,9 +13,8 @@ npm-debug.log /quick-start # tests -/test /coverage /.nyc_output # dist -/dist \ No newline at end of file +/dist diff --git a/sample/13-mongo-typeorm/test/jest-e2e.json b/sample/13-mongo-typeorm/test/jest-e2e.json new file mode 100644 index 00000000000..f6735092114 --- /dev/null +++ b/sample/13-mongo-typeorm/test/jest-e2e.json @@ -0,0 +1,13 @@ +{ + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": ".", + "testEnvironment": "node", + "testRegex": ".e2e-spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + } +} diff --git a/sample/13-mongo-typeorm/test/photos.e2e-spec.ts b/sample/13-mongo-typeorm/test/photos.e2e-spec.ts new file mode 100644 index 00000000000..442cc2d986b --- /dev/null +++ b/sample/13-mongo-typeorm/test/photos.e2e-spec.ts @@ -0,0 +1,30 @@ +import { INestApplication } from '@nestjs/common'; +import { Test } from '@nestjs/testing'; +import * as request from 'supertest'; +import { AppModule } from '../src/app.module'; + +describe('Photos (e2e)', () => { + let app: INestApplication; + + beforeAll(async () => { + const moduleFixture = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + app = moduleFixture.createNestApplication(); + await app.init(); + }); + + afterAll(async () => { + if (app) { + await app.close(); + } + }); + + it('should return an empty list of photos', async () => { + await request(app.getHttpServer()) + .get('/photo') + .expect(200) + .expect(res => expect(res.body) + .toEqual([])); + }); +}); From 265dfb30fceb549ccf2da6a5fba9000a1515d9a6 Mon Sep 17 00:00:00 2001 From: Kim Kern Date: Sat, 12 Oct 2019 23:49:58 +0200 Subject: [PATCH 3/3] doc(samples) add tests to readme --- sample/13-mongo-typeorm/README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sample/13-mongo-typeorm/README.md b/sample/13-mongo-typeorm/README.md index 382469cf616..b99c2aa1ec1 100644 --- a/sample/13-mongo-typeorm/README.md +++ b/sample/13-mongo-typeorm/README.md @@ -1,4 +1,4 @@ -### Mongoose sample +### Mongo + Typeorm sample ### Installation @@ -23,4 +23,8 @@ After running the sample, you can stop the Docker container with Then, run Nest as usual: -`npm run start` \ No newline at end of file +`npm run start` + +### Run the tests +* Unit tests: `npm run test` +* e2e tests: `npm run test:e2e`