I Began writing a some tests to our application be I seem to have some trouble with it's performance.
This test alone takes up to 10.842s! (according to jest output)
It seems to be something with injecting the mocked repository, since the standard test that comes with the nest CLI ran smoothly. But since I mocked the whole repository i shouldn't have any latency involved with the DB connection, so I stand confused.
import { ApplicationService } from '../application.service';
import { ApplicationRepository } from '../application.repository';
import { Test } from '@nestjs/testing';
import { ApplicationController } from '../application.controller';
import { ApplicationDTO } from '../DTO/applicationDTO';
describe('ApplicationService', () => {
let service: ApplicationService;
let mockRepo;
beforeEach(async () => {
mockRepo = {
applications: [
{id: 1, type: 'DATABASE', name: 'WhatsApp', access: 'ACCESS', user_access: 'HIGH', group: 'Nice', connection: 'aaaaaaaaaaaaaaa'},
{id: 2, type: 'DATABASE', name: 'Telegram', access: 'ACCESS', user_access: 'LOW', group: 'Badguy', connection: 'aaaaaaaaaaaaaaa'},
],
find() {
return this.applications;
},
create(application) {
this.applications.push(application);
return { save() {
return;
}};
},
delete(id) {
this.applications = this.applications.splice(0, id);
},
};
const mockRepositoryProvider = {
provide: ApplicationRepository,
useValue: mockRepo,
};
const module = await Test.createTestingModule({
controllers: [ApplicationController],
providers: [ApplicationService, mockRepositoryProvider],
}).compile();
service = module.get<ApplicationService>(ApplicationService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
it('should return all applications', () => {
expect(service.findAll()).toBe(mockRepo.applications);
});
it('should add a application', () => {
const application = new ApplicationDTO(
{ id: 4, type: 'DATABASE', name: 'WhatsApp', access: 'ACCESS', user_access: 'ACCESS', group: 'Nice', connection: 'aaaaaaaaaaaaaaa'},
);
service.add(application);
expect(service.findAll()).toContainEqual(
expect.objectContaining(
{ id: 4, type: 'DATABASE', name: 'WhatsApp', access: 'ACCESS', user_access: 'ACCESS', group: 'Nice', connection: 'aaaaaaaaaaaaaaa'},
));
});
});
I guess, it's okay. About 10 second takes nest app to launch and if you add more tests the time will not increase significantly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With