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

Commit b7f3d04

Browse files
IgorMinarvicb
authored andcommitted
fix: always run jasmine's done callbacks for async tests in jasmine's 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
1 parent 531d0ec commit b7f3d04

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

karma-microtasks.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = function (config) {
77
'test/util.js',
88
'dist/zone-microtask.js',
99
'dist/*-zone.js',
10+
'test/jasmine-patch.js',
1011
//'test/lib/brick.js',
1112
'test/microtasks.spec.js',
1213
{pattern: 'test/assets/**/*.html', watched: true, served: true, included: false}

karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = function (config) {
77
'test/util.js',
88
'dist/zone.js',
99
'dist/*-zone.js',
10+
'test/jasmine-patch.js',
1011
//'test/lib/brick.js',
1112
'test/**/*.spec.js',
1213
{pattern: 'test/assets/**/*.html', watched: true, served: true, included: false}

test/commonjs.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict';
22

3+
// WARNING: Browserify tests currently don't patch Jasmine's `it` and `fit` functions.
4+
// Writing any async tests in this file will result in zone leakage.
5+
// See `jasmine-patch.js` for a hint of what needs to be done here.
6+
7+
38
describe('Zone in CommonJS environment', function () {
49
var commonJSExports;
510

test/jasmine-patch.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
(function() {
4+
// patch jasmine's it and fit functions so that the `done` callback always resets the zone
5+
// to the jasmine zone, which should be the root zone. (angular/zone.js#91)
6+
7+
var jasmineZone = window.zone;
8+
var originalIt = window.it;
9+
var originalFit = window.fit;
10+
11+
window.it = function zoneResettingIt(description, specFn) {
12+
if (specFn.length) {
13+
originalIt(description, function zoneResettingSpecFn(originalDone) {
14+
specFn(function zoneResettingDone() {
15+
jasmineZone.run(originalDone);
16+
});
17+
});
18+
} else {
19+
originalIt(description, specFn);
20+
}
21+
};
22+
23+
window.fit = function zoneResettingFit(description, specFn) {
24+
if (specFn.length) {
25+
originalFit(description, function zoneResettingSpecFn(originalDone) {
26+
specFn(function zoneResettingDone() {
27+
jasmineZone.run(originalDone);
28+
});
29+
});
30+
} else {
31+
originalFit(description, specFn);
32+
}
33+
};
34+
35+
}(window));
36+
37+
// global beforeEach to check if we always start from the root zone
38+
beforeEach(function() {
39+
expect(window.zone.isRootZone()).toBe(true);
40+
});

0 commit comments

Comments
 (0)