Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(sample-01): added service unit tests #10623

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions sample/01-cats-app/src/cats/cats.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ describe('CatsController', () => {
expect(await catsController.findAll()).toBe(result);
});
});

describe('create', () => {
it('should add a new cat', async () => {
const cat: Cat = {
age: 2,
breed: 'Bombay',
name: 'Pixel',
};
const expectedCatArray = [cat];

expect(await catsController.findAll()).toStrictEqual([]);
Yansb marked this conversation as resolved.
Show resolved Hide resolved

await catsController.create(cat);

expect(await catsController.findAll()).toStrictEqual(expectedCatArray);
});
});
});
46 changes: 46 additions & 0 deletions sample/01-cats-app/src/cats/cats.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Test } from '@nestjs/testing';
import { CatsService } from './cats.service';
import { Cat } from './interfaces/cat.interface';

describe('CatsService', () => {
let catsService: CatsService;

beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [CatsService],
}).compile();

catsService = moduleRef.get<CatsService>(CatsService);
});

describe('findAll', () => {
it('should return an array of cats', async () => {
const result = [
{
name: 'Frajola',
age: 2,
breed: 'Stray',
},
];
jest.spyOn(catsService, 'findAll').mockImplementation(() => result);
Yansb marked this conversation as resolved.
Show resolved Hide resolved

expect(await catsService.findAll()).toBe(result);
});
});
describe('create', () => {
it('should add a new cat', async () => {
const cat: Cat = {
name: 'Frajola',
age: 2,
breed: 'Stray',
};
const expectedCatArray = [cat];

expect(await catsService.findAll()).toStrictEqual([]);
Yansb marked this conversation as resolved.
Show resolved Hide resolved

await catsService.create(cat);

expect(await catsService.findAll()).toStrictEqual(expectedCatArray);
Yansb marked this conversation as resolved.
Show resolved Hide resolved
});
});
});