我开始为我们的应用程序编写一些测试,因为我似乎对其性能有麻烦。
仅此一项测试就最多需要10.842s! (根据笑话输出)
这似乎与注入模拟存储库有关,因为Nest CLI随附的标准测试运行顺利。但是,由于我嘲笑了整个存储库,因此数据库连接不应该涉及任何延迟,所以我感到困惑。
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'},
));
});
});
答案 0 :(得分:0)
我想,没关系。 Nest应用程序大约需要10秒钟才能启动,如果您添加更多测试,则时间不会明显增加。