【发布时间】:2021-06-02 17:44:22
【问题描述】:
在nestjs中,测试文件(来自cli的模板)在beforeEach内有Test.createTestingModule,因此它在每次测试之前重新创建模块。
例如foo.service.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { FooService } from './foo.service';
describe('FooService', () => {
let service: FooService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [FooService],
}).compile();
service = module.get<FooService>(FooService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
但是为什么它在beforeEach而不是beforeAll?
有什么理由让它出现在beforeEach 中吗?也许会引起问题?
我看到在e2e测试中,应用程序是在beforeAll中创建的,所以我不确定为什么在beforeEach中创建单元测试。
【问题讨论】: