Skip to content

Commit

Permalink
Merge pull request nestjs#8037 from Tony133/test/sample-13-unit-test
Browse files Browse the repository at this point in the history
tests(sample-13): added unit tests
  • Loading branch information
kamilmysliwiec authored Oct 7, 2021
2 parents 0a372c2 + 60e18f5 commit 4e31899
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sample/13-mongo-typeorm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions sample/13-mongo-typeorm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
68 changes: 68 additions & 0 deletions sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(PhotoController);
service = module.get<PhotoService>(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,
},
]);
});
});
});
51 changes: 51 additions & 0 deletions sample/13-mongo-typeorm/src/photo/photo.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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<Photo>;

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>(PhotoService);
repository = module.get<Repository<Photo>>(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);
});
});

0 comments on commit 4e31899

Please sign in to comment.