Solvedangular Issue with mocha, TestBed : TypeError: Cannot read property 'assertPresent' of undefined

I'm submitting a ...

[x] bug report

Current behavior
When running tests, I get the error:

> mocha build/test
  ODataService -
    1) "before each" hook for "Construct via injection"

  0 passing (41ms)
  1 failing

  1)  "before each" hook for "Construct via injection":
     TypeError: Cannot read property 'assertPresent' of undefined
      at resetFakeAsyncZone (C:\Users\azureuser\Documents\GitHub\_forks\angular2-odata\node_modules\@angular\core\bundles\core-testing.umd.js:385:22)
      at Context.<anonymous> (C:\Users\azureuser\Documents\GitHub\_forks\angular2-odata\node_modules\@angular\core\bundles\core-testing.umd.js:1255:13)
      at callFn (C:\Users\azureuser\Documents\GitHub\_forks\angular2-odata\node_modules\mocha\lib\runnable.js:334:21)
      at Hook.Runnable.run (C:\Users\azureuser\Documents\GitHub\_forks\angular2-odata\node_modules\mocha\lib\runnable.js:327:7)
      at next (C:\Users\azureuser\Documents\GitHub\_forks\angular2-odata\node_modules\mocha\lib\runner.js:299:10)
      at Immediate.<anonymous> (C:\Users\azureuser\Documents\GitHub\_forks\angular2-odata\node_modules\mocha\lib\runner.js:327:5)

My test looks like:

require('zone.js');
import 'reflect-metadata';
import { assert } from 'chai';
import { Observable, Operator } from 'rxjs/rx';
import { Location } from '@angular/common';
import { inject, TestBed } from '@angular/core/testing';
// import { TestBedHelper } from './testbedHelper';
import { MockBackend } from '@angular/http/testing';
import { BaseRequestOptions, Http, ConnectionBackend, HttpModule } from '@angular/http';
import { IEmployee } from './employee';
import { ODataOperation } from '../operation';
import { ODataServiceFactory } from '../odataservicefactory';
import { ODataConfiguration } from '../config';

describe('ODataService - ', () => {
    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                BaseRequestOptions,
                MockBackend,
                {
                    provide: Http, useFactory: (backend: ConnectionBackend, defaultOptions: BaseRequestOptions) => {
                        return new Http(backend, defaultOptions);
                    },
                    deps: [MockBackend, BaseRequestOptions]
                },
                ODataConfiguration,
                ODataServiceFactory
            ],
            imports: [
                HttpModule
            ]
        });
    });

    it('Construct via injection', inject([ ODataServiceFactory ], (factory: ODataServiceFactory) => {
        // Act
        let service = factory.CreateService<IEmployee>('Employees');

        // Assert
        assert.isNotNull(service);
    }));
});

Please tell us about your environment:

  • Mocha version: 3.0.2
  • Angular version: 2.0.0-rc.6
  • Language: [all | TypeScript | ES6/7 | ES5]
15 Answers

✔️Accepted Answer

// tslint:disable:ordered-imports
import 'zone.js/dist/long-stack-trace-zone'
import 'zone.js/dist/proxy.js'
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'

Fixed it for me

Other Answers:

Hi! I had exactly the same TypeError using Karma test runner with Angular2. What helped me was to include all zone.js files listed here: https://github.com/angular/quickstart/blob/master/karma.conf.js#L37 in my karma.conf.js file. I see you use require to load zone.js, maybe you need to require all of this files?

The most important thing is to keep import section like it was right after "ng new myproject".
For me import 'zone.js/dist/zone-testing' should be at the top of all imports:

import 'zone.js/dist/zone-testing';
// tslint:disable-next-line:ordered-imports
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

declare const require: any;

// 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);

I solved the problem by importing zone.js/dist/proxy into src/test.ts file. Also had to change the order of imports a bit like so - import proxy and jasmine after sync-test.

import 'zone.js/dist/long-stack-trace-zone';
-import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import 'zone.js/dist/sync-test';
+import 'zone.js/dist/proxy';
+import 'zone.js/dist/jasmine-patch';

The error TypeError: Cannot read property 'assertPresent' of undefined now disappears.

Test Environment

  • angular 2.0.0-rc.6
  • zone.js 0.6.17

More Issues: