单元测试 – 使用TestBed进行Angular2测试获取区域未定义错误

我一直在研究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]

        });
    });

});

最佳答案 我通过在我的配置中添加以下内容来修复我的项目.

我正在使用Wallaby.js来解决这个问题,我将以下行添加到我的module.exports中

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

如果您正在使用像业力这样的东西,则应该应用相同的解决方案.

点赞