From fec5a25bdde547cfc48fbf1185dd1f01564e708d Mon Sep 17 00:00:00 2001 From: Tony133 Date: Sun, 5 Sep 2021 22:42:35 +0200 Subject: [PATCH 1/2] tests(sample-13): added unit tests --- sample/13-mongo-typeorm/package-lock.json | 10 +++ sample/13-mongo-typeorm/package.json | 18 +++++ .../src/photo/photo.controller.spec.ts | 68 +++++++++++++++++++ .../src/photo/photo.service.spec.ts | 51 ++++++++++++++ 4 files changed, 147 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 3187750cc72..4bf43050d1b 100644 --- a/sample/13-mongo-typeorm/package-lock.json +++ b/sample/13-mongo-typeorm/package-lock.json @@ -1743,6 +1743,16 @@ "@types/istanbul-lib-report": "*" } }, + "@types/jest": { + "version": "27.0.1", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.0.1.tgz", + "integrity": "sha512-HTLpVXHrY69556ozYkcq47TtQJXpcWAWfkoqz+ZGz2JnmZhzlRjprCIyFnetSy8gpDWwTTGBcRVv1J1I1vBrHw==", + "dev": true, + "requires": { + "jest-diff": "^27.0.0", + "pretty-format": "^27.0.0" + } + }, "@types/json-schema": { "version": "7.0.8", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.8.tgz", diff --git a/sample/13-mongo-typeorm/package.json b/sample/13-mongo-typeorm/package.json index c68c9274342..9f133696ea4 100644 --- a/sample/13-mongo-typeorm/package.json +++ b/sample/13-mongo-typeorm/package.json @@ -34,6 +34,7 @@ "@nestjs/schematics": "8.0.3", "@nestjs/testing": "8.0.6", "@types/express": "4.17.13", + "@types/jest": "27.0.1", "@types/node": "14.17.14", "@types/supertest": "2.0.11", "@typescript-eslint/eslint-plugin": "4.29.3", @@ -49,5 +50,22 @@ "ts-node": "10.2.1", "tsconfig-paths": "3.11.0", "typescript": "4.3.5" + }, + "jest": { + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": "src", + "testRegex": ".*\\.spec\\.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + }, + "collectCoverageFrom": [ + "**/*.(t|j)s" + ], + "coverageDirectory": "../coverage", + "testEnvironment": "node" } } 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..42f53d12bf9 --- /dev/null +++ b/sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts @@ -0,0 +1,68 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PhotoController } from './photo.controller'; +import { PhotoService } from './photo.service'; + +describe('Photo Controller', () => { + let controller: PhotoController; + let service: PhotoService; + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [PhotoController], + providers: [ + { + provide: PhotoService, + useValue: { + findAll: jest.fn().mockResolvedValue([ + { + name: 'Photo #1', + description: 'Description #1', + filename: 'Filename #1', + isPublish: true, + }, + { + name: 'Photo #2', + description: 'Description #2', + filename: 'Filename #2', + isPublish: true, + }, + { + name: 'Photo #3', + description: 'Description #3', + filename: 'Filename #3', + isPublish: false, + }, + ]), + }, + }, + ], + }).compile(); + + controller = module.get(PhotoController); + service = module.get(PhotoService); + }); + + describe('findAll()', () => { + it('should get an array of photos', () => { + expect(controller.findAll()).resolves.toEqual([ + { + name: 'Photo #1', + description: 'Description #1', + filename: 'Filename #1', + isPublish: true, + }, + { + name: 'Photo #2', + description: 'Description #2', + filename: 'Filename #2', + isPublish: true, + }, + { + name: 'Photo #3', + description: 'Description #3', + filename: 'Filename #3', + isPublish: false, + }, + ]); + }); + }); +}); 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..a47dbc0c6a2 --- /dev/null +++ b/sample/13-mongo-typeorm/src/photo/photo.service.spec.ts @@ -0,0 +1,51 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { Photo } from './photo.entity'; +import { PhotoService } from './photo.service'; + +describe('CatService', () => { + let service: PhotoService; + let repository: Repository; + + const photosArray = [ + { + name: 'Photo #1', + description: 'Description #1', + filename: 'Filename #1', + isPublish: true, + }, + { + name: 'Photo #2', + description: 'Description #2', + filename: 'Filename #2', + isPublish: true, + }, + ]; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + PhotoService, + { + provide: getRepositoryToken(Photo), + useValue: { + find: jest.fn().mockResolvedValue(photosArray), + }, + }, + ], + }).compile(); + + service = module.get(PhotoService); + repository = module.get>(getRepositoryToken(Photo)); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); + + it('should return an array of photos', async () => { + const photos = await service.findAll(); + expect(photos).toEqual(photosArray); + }); +}); From 60e18f547acfe1166c30b0ec74e384e6f8d6a6e6 Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Thu, 7 Oct 2021 09:00:54 +0200 Subject: [PATCH 2/2] Update sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts --- sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts b/sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts index 42f53d12bf9..6d830d2630d 100644 --- a/sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts +++ b/sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts @@ -42,7 +42,7 @@ describe('Photo Controller', () => { }); describe('findAll()', () => { - it('should get an array of photos', () => { + it('should return an array of photos', () => { expect(controller.findAll()).resolves.toEqual([ { name: 'Photo #1',