Recently, I've been following the tutorial on https://angular.io/docs/ts/latest/guide/testing.html to create my first set of unit tests. Everything was going well until I reached the TestBed example. That's where things took a turn and I encountered an error: 'Uncaught ReferenceError: Zone is not defined'.
In my spec-bundle file, I made sure to declare and initialize the Testbed environment properly.
Spec-bundle setup:
Error.stackTraceLimit = Infinity;
require('phantomjs-polyfill');
require('core-js/es6');
require('core-js/es7/reflect');
// Typescript emit helpers polyfill
require('ts-helpers');
// Dependencies in specific order
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');
const testing = require('@angular/core/testing');
const browser = require('@angular/platform-browser-dynamic/testing');
testing.TestBed.initTestEnvironment(
browser.BrowserDynamicTestingModule,
browser.platformBrowserDynamicTesting()
);
Object.assign(global, testing);
window.__karma__ && require('./karma-require');
Test file snippet:
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]
});
});
End of test file excerpt.