diff --git a/sample/13-mongo-typeorm/package-lock.json b/sample/13-mongo-typeorm/package-lock.json index 95015b86d44..53027149647 100644 --- a/sample/13-mongo-typeorm/package-lock.json +++ b/sample/13-mongo-typeorm/package-lock.json @@ -1769,6 +1769,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 6e881db394f..ff8f4b1e9c7 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.11", "@types/express": "4.17.13", + "@types/jest": "27.0.1", "@types/node": "14.17.21", "@types/supertest": "2.0.11", "@typescript-eslint/eslint-plugin": "4.33.0", @@ -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..6d830d2630d --- /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 return 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); + }); +});