Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: always run jasmine's done callbacks for async tests in jasmine's…
Browse files Browse the repository at this point in the history
… zone

If we don't, we are leaking zones because Jasmine executes async tests
from within the done callback,
which means we never unwind the zone stack.

This causes various specs to fail if other specs run before them and
build up a zone stack.

Closes #91
  • Loading branch information
IgorMinar authored and vicb committed May 8, 2015
1 parent 531d0ec commit b7f3d04
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions karma-microtasks.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function (config) {
'test/util.js',
'dist/zone-microtask.js',
'dist/*-zone.js',
'test/jasmine-patch.js',
//'test/lib/brick.js',
'test/microtasks.spec.js',
{pattern: 'test/assets/**/*.html', watched: true, served: true, included: false}
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function (config) {
'test/util.js',
'dist/zone.js',
'dist/*-zone.js',
'test/jasmine-patch.js',
//'test/lib/brick.js',
'test/**/*.spec.js',
{pattern: 'test/assets/**/*.html', watched: true, served: true, included: false}
Expand Down
5 changes: 5 additions & 0 deletions test/commonjs.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict';

// WARNING: Browserify tests currently don't patch Jasmine's `it` and `fit` functions.
// Writing any async tests in this file will result in zone leakage.
// See `jasmine-patch.js` for a hint of what needs to be done here.


describe('Zone in CommonJS environment', function () {
var commonJSExports;

Expand Down
40 changes: 40 additions & 0 deletions test/jasmine-patch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

(function() {
// patch jasmine's it and fit functions so that the `done` callback always resets the zone
// to the jasmine zone, which should be the root zone. (angular/zone.js#91)

var jasmineZone = window.zone;
var originalIt = window.it;
var originalFit = window.fit;

window.it = function zoneResettingIt(description, specFn) {
if (specFn.length) {
originalIt(description, function zoneResettingSpecFn(originalDone) {
specFn(function zoneResettingDone() {
jasmineZone.run(originalDone);
});
});
} else {
originalIt(description, specFn);
}
};

window.fit = function zoneResettingFit(description, specFn) {
if (specFn.length) {
originalFit(description, function zoneResettingSpecFn(originalDone) {
specFn(function zoneResettingDone() {
jasmineZone.run(originalDone);
});
});
} else {
originalFit(description, specFn);
}
};

}(window));

// global beforeEach to check if we always start from the root zone
beforeEach(function() {
expect(window.zone.isRootZone()).toBe(true);
});

0 comments on commit b7f3d04

Please sign in to comment.