使用TestBed进行Angular2测试获取Zone undefined错误

时间:2016-12-07 22:03:23

标签: unit-testing typescript jasmine karma-runner testbed

我一直在使用Angular教程https://angular.io/docs/ts/latest/guide/testing.html来构建我的第一个单元测试。在进入TestBed示例之前,我能够完成所有工作。当我将TestBed添加到混合中时,我得到'Uncaught ReferenceError:Zone is not defined'。

在我的spec-bundle中,I区声明并初始化了Testbed环境。

规格束

Error.stackTraceLimit = Infinity;

require('phantomjs-polyfill');

require('core-js/es6');
require('core-js/es7/reflect');

// Typescript emit helpers polyfill
require('ts-helpers');

// DO NOT REORDER: Dependency order needs to be strictly followed
require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
require('zone.js/dist/sync-test');
require('zone.js/dist/proxy');
require('zone.js/dist/jasmine-patch');

// RxJS
require('rxjs/Rx');

var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.TestBed.initTestEnvironment(
    browser.BrowserDynamicTestingModule,
    browser.platformBrowserDynamicTesting()
);

Object.assign(global, testing);

window.__karma__ && require('./karma-require');

测试文件

import { ComponentFixture, TestBed } from '@angular/core/testing';   
import { SomeComponent } from './some.component';

let fixture: ComponentFixture<SomeComponent>;

describe('Orders Component', () => {

    let ordersComponentStub: SomeComponent;

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [SomeComponent]

        });
    });

});

1 个答案:

答案 0 :(得分:1)

我通过在配置中添加以下内容在我的项目中修复了此问题。 我正在使用Wallaby.js以解决这个问题我将以下行添加到我的module.exports

{pattern:'node_modules / zone.js / dist / zone.js',包括:true,watched:true}

如果你正在使用像业力这样的东西,那么同样的解决方案应该适用。