question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
ghostcommented, Sep 6, 2017
// 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

5reactions
vundercommented, Sep 6, 2018

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);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing | Cannot read property 'assertPresent' of undefined at ...
This error occurred when a Jasmine test was using fakeAsync and tick. Removing fakeAsync/tick resolved this issue.
Read more >
cannot read properties of undefined angular testing - You.com
Throwing and error TypeError: Cannot read properties of undefined (reading 'getModels'). Open side panel. Angular testing Cannot read property 'name' of ...
Read more >
How to fix "Cannot read properties of undefined (reading ...
I want to iterate the app works perfectly, this is only a problem when running unit tests. We're using the MatDatepickerModule with a...
Read more >
JavaScript — Unit Testing using Mocha and Chai - codeburst
This article will cover testing of basic function, testing of async callback functions and testing of promises with Mocha and Chai.
Read more >
How To Use waitForAsync and fakeAsync with Angular Testing
When the button is clicked, the title property is set using a promise. ... import { TestBed, waitForAsync } from '@angular/core/testing'; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found