我的项目是使用 nx 原理图创建的,并且我在库中有一些组件,我想使用 jest.js 对其进行单元测试。每个测试都失败并出现以下错误:
● MyComponent › should create
Failed: "Zone is needed for the async() test helper but could not be found. Please make sure that your environment includes zone.js/dist/zone.js"
7 | let fixture: ComponentFixture<MyComponent>;
8 |
> 9 | beforeEach(async(() => {
| ^
10 | TestBed.configureTestingModule({
11 | declarations: [ MyComponent ]
12 | })
at Env.beforeEach (node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:41:24)
at Suite.<anonymous> (libs/componentlib/src/lib/components/my-component/my-component.component.spec.ts:9:3)
at Object.<anonymous> (libs/componentlib/src/lib/components/my-component/my-component.component.spec.ts:5:1)
● MyComponent › should create
TypeError: Cannot read property 'getComponentFromError' of null
at TestBedViewEngine._initIfNeeded (../../packages/core/testing/src/test_bed.ts:393:46)
at TestBedViewEngine.createComponent (../../packages/core/testing/src/test_bed.ts:594:10)
at Function.TestBedViewEngine.createComponent (../../packages/core/testing/src/test_bed.ts:247:36)
at Object.<anonymous> (libs/componentlib/src/lib/components/my-component/my-component.component.spec.ts:17:23)
● MyComponent › should create
expect(received).toBeTruthy()
Received: undefined
21 |
22 | it('should create', () => {
> 23 | expect(component).toBeTruthy();
| ^
24 | });
25 | });
26 |
at Object.<anonymous> (libs/componentlib/src/lib/components/my-component/my-component.component.spec.ts:23:23)
我已经尝试在规范文件中导入 zone.js、导入模块、删除异步、在每次测试之前重置测试环境,但一切都失败了,并出现了一些不同的错误。我还应该提到我正在使用来自 vmware 的清晰组件。
这是 .spec 文件:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MyComponent } from './my-component.component';
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我期待在使用 nx 时这应该像预期的那样工作。我在这里想念什么?