首页 > 解决方案 > zone.js 预计将在 ProxyZone 中运行

问题描述

我觉得这个问题已经被问过很多次了,但是我找到的解决方案都没有帮助,我有点迷失了我的问题的原因。解决方案通常建议将 async/fakeAsync 调用设置在错误的位置(例如,describe 而不是 beforeEach)或完全删除 async 但这些都不起作用,因为这是一个普通生成的测试,我希望它只是跑。

因此,无论我想运行什么测试用例,我都面临以下问题:

Error: Expected to be running in 'ProxyZone', but it was not found.
        at Function.assertPresent (node_modules/zone.js/dist/zone-testing.js:215:23)
        at Object.resetFakeAsyncZone (node_modules/zone.js/dist/zone-testing.js:1997:54)
        at resetFakeAsyncZone (packages/core/testing/src/fake_async.ts:23:32)
        at UserContext.<anonymous> (packages/core/testing/src/before_each.ts:26:5)
        at <Jasmine>

我尝试了一个完全空且新生成的组件,其中测试用例是由 cli 自动生成的,它也失败了。这是测试用例组件测试:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TestCaseComponent } from './test-case.component';

describe('TestCaseComponent', () => {
   let component: TestCaseComponent;
   let fixture: ComponentFixture<TestCaseComponent>;

beforeEach(async () => {
   await TestBed.configureTestingModule({
      declarations: [ TestCaseComponent ]
 })
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(TestCaseComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

 it('should create', () => {
 expect(component).toBeTruthy();
  });
});

我使用以下命令运行测试:

ng 测试 --include=**/test-case.component.spec.ts

我正在使用 zone.js 版本 0.11.4

标签: angularzone.js

解决方案


好吧,只是我在这里发布了我的问题,我想通了......在解决问题的几次尝试之一中,我添加了这一行

import 'zone.js/dist/zone-testing.js'

到我的 pollyfills.ts 文件。

但是这里似乎不需要它,必须在 test.ts 文件的最开始添加。


推荐阅读