angular: RC6 unit tests with mocha fail to run

I’m submitting a … (check one with “x”)

[*] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

No tests run. It was working fine in RC5 and all breaking changes have been addressed. The error given is:

Expected to be running in 'ProxyZone', but it was not found.
assertPresent@test.ts:6620:98
resetFakeAsyncZone@test.ts:25346:37
test.ts:26216:32

This is thrown before the it block is executed. Switching from mocha to jasmine + adding the zone jasmine patch fixes the issue as a workaround.

Expected/desired behavior Tests should run with mocha

Reproduction of the problem

I’ve put together a repo which reproduces the error here: https://github.com/mattlewis92/angular2-testing-bug/blob/master/test.ts

What is the motivation / use case for changing the behavior?

N/A

Please tell us about your environment:

  • Angular version: 2.0.0-rc.6
  • Browser: [PhantomJS 2]
  • Language: [TypeScript 1.8]

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 49 (23 by maintainers)

Most upvoted comments

We’ll be cutting a release of zone.js within the next few days. Once that happens you’ll be able to get the patch from the npm package then.

On Wed, Nov 16, 2016 at 9:55 AM nording notifications@github.com wrote:

There is a Mocha-patch available, https://github.com/angular/zone.js/tree/master/lib/mocha, it works well with the test utils like async and fakeAsync.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/angular/issues/11230#issuecomment-261020454, or mute the thread https://github.com/notifications/unsubscribe-auth/AANM6FO2nyz7K-JTJOW9dQ9ujXrCMHn8ks5q-0OHgaJpZM4JyepH .

There is a Mocha-patch available, https://github.com/angular/zone.js/tree/master/lib/mocha, it works well with the test utils like async and fakeAsync.

Doing the following after calling TestBed.initTestEnvironment is also a viable workaround if one doesn’t use async or fakeAsync:

import { getTestBed } from "@angular/core/testing";

const hook = new Mocha.Hook("Modified Angular beforeEach Hook", () => {
    getTestBed().resetTestingModule();
});
hook.ctx = mocha.suite.ctx;
hook.parent = mocha.suite;
mocha.suite._beforeEach = [hook];

In fact if you don’t use async or fakeAsync helpers then you can write tests in mocha.

No. Even when I use only describe and it , it fails.

import 'core-js';
import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';
import 'rxjs';
import {
  TestBed
} from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
import {assert} from 'chai';

TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());

describe('test demo', () => {
  it('should ok', () => {
    console.log('0');
    assert({ foo: 'foo' }.foo === { foo: 'foo' }.foo);
  });
});
PhantomJS 2.1.1 (Mac OS X 0.0.0)  "before each" hook for "should ok" FAILED
    undefined is not an object (evaluating 'ProxyZoneSpec.assertPresent')
    resetFakeAsyncZone@test.ts:27523:23
    test.ts:28393:32
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.013 secs / 0.007 secs)

Then I added import 'zone.js/dist/proxy';, but it still failed.

PhantomJS 2.1.1 (Mac OS X 0.0.0)  "before each" hook for "should ok" FAILED
    Expected to be running in 'ProxyZone', but it was not found.
    assertPresent@test.ts:9455:98
    resetFakeAsyncZone@test.ts:27686:37
    test.ts:28556:32
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.012 secs / 0.007 secs)
npm ERR! Test failed.  See above for more details.

Also, import 'zone.js/dist/sync-test'; didn’t solve anything.

The problem we should solve is that A2 is depending on a specific testing framework by the default.

My imports in my spec-bundle.js file look like this and work fine:

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

As this is currently the first google result for “Expected to be running in ‘ProxyZone’, but it was not found”, I’ll offer a solution unrelated to the report above.

I stupidly had my fakeAsync() call around the describe callback block instead of the it callback. Took me a little head scratching to figure it out, so maybe this will save others some time.

I got it working somehow using “interfaces” extension feature of Mocha. Refer to this gist for details.

It’s a quick initial try with some missing bits, but it works with my tests. The biggest missing part is a counterpart to jasmine QueueRunner patching. Once I figure out how to deal with it, I will put this to an npm module, I guess.

Hey, @HichamBI I’m using mocha with jsdom without any issues. Though I don’t use the “official” patch, but rather my own – see my earlier comment in this thread.

I also copy properties from jsdom’s window to global and set global.window – everything before requiring zone.js and company.

Hope this helps.

@laco0416, I’m writing mocha test for a small library and don’t need async or fakeAsync so I skip the whole TestBed.initTestEnvironment which works fine.

require('core-js/es7/reflect');
require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
Error.stackTraceLimit = 5;

@juliemr What do you think about this?

No, I don’t want to use karma and phantomjs if it possible. I want also understand why it not working with the new Zone.js release.

Confirmed that the latest zone.js version mocha patch works perfectly, nice work @nording !