2
votes

I am executing unit tests for angular projects but I got error

'Uncaught ReferenceError: Zone is not defined'

in Jasmine and Karma. Currently I using angular 6.

"jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "typescript": "2.7.2",

Uncaught ReferenceError: Zone is not defined at :9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:85 at :9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:9 at Object. (:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:12) at Object../node_modules/zone.js/dist/zone-testing.js (zone-testing.js:1584) at webpack_require (:9876/_karma_webpack_/webpack:/webpack/bootstrap:76) at Object../src/test.ts (:9876/_karma_webpack_/webpack:/src/test.ts:3) at webpack_require (:9876/_karma_webpack_/webpack:/webpack/bootstrap:76) at checkDeferredModules (:9876/_karma_webpack_/webpack:/webpack/bootstrap:43) at :9876/_karma_webpack_/webpack:/webpack/bootstrap:134 at bootstrap:134

3

3 Answers

3
votes

I had the same issue in Angular 9.

I've added some imports and one polyfill for window to "test.ts" file. Below the full file.

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

(window as any).global = window;

declare const require: {
  context(path: string, deep?: boolean, filter?: RegExp): {
    keys(): string[];
    <T>(id: string): T;
  };
};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
0
votes

go through below link, it would be helpful for you.

https://github.com/angular/zone.js/issues/776

module.exports = function (config) {
config.set({
basePath: '../',
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular/cli/plugins/karma')
],
0
votes

I do not know if this is still an issue, but if everything seems fine and it still does not work, try the following in the karma.conf.js file:

config.set({... files: ['node_modules/zone.js/dist/zone.js'] });

For further info, see this link: Link