jest.mock('../../../api/src/user/user.service', () => ({...}));
jest.mock('../../../api/src/color/color.service', () => ({...}));
jest.mock('../../../api/src/record/record.service', () => ({...}));
jest.mock('../../../api/src/track/track.service', () => ({...}));
...
Change function -> new output / arguments
-> fix test
const updateSpy = jest.spyOn(projectService, 'update')
.mockImplementation(async () => expectedResult as unknown as ProjectDto);
// step #1 - initialize API
let app: INestApplication;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
})
.overrideGuard(AuthGuard)
.useClass(AuthGuardMock)
.compile();
app = moduleRef.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
});
// step #2 - mock authentication
class AuthGuardMock implements CanActivate {
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
request.user = testUsers[0];
return true;
}
}
// step #3 - call API and test
await request(app.getHttpServer())
.post(`/projects/${testProject.id}/tasks`)
.send({
details: 'test task text',
userId: testId,
dueDate: testDueDate,
})
.expect(201);
// step #4 - verify data
await request(app.getHttpServer())
.get(`/project/${testProject.id}`)
.send()
.expect(200)
.expect((res: Response) => {
const data: ProjectDto = res.body;
expect(data.id).toBe(testProject.id);
...
});
Date.now = (): number => {
return 1635792963551;
};
const mockGlobalDate = (): Date => {
const dateMock = new Date(1635792963551);
global.Date = class DateMock extends Date {
constructor(...args: any[]) {
if (args.length) {
// @ts-ignore
super(...args);
} else {
super(dateMock.getTime());
}
}
} as any;
Date.now = (): number => {
return dateMock.getTime();
};
};