Skip to content

Commit de86285

Browse files
committed
feat(zone.js): include jasmine describe block name when raising unexpected task error
As mentioned in the previous commit that ensured that the Zone name is included in errors raised by the `SyncTestZoneSpec`, we can now include the Jasmine describe block descriptions in such errors. Errors can often happen when users accidentally try to set up Angular tests without an `it` block. Resulting in errors where it's not clear at all which describe block (of potentially a large repository) is involved: ``` An error was thrown in afterAll error properties: Object({ originalStack: 'Error: Cannot call XX from within a sync test. at new ZoneAwareError (packages/zone.js/test/browser_test_rollup.umd.js:98:37) at e.onScheduleTask (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:158:196) at e.scheduleTask (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:14:7529) at t.scheduleTask (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:14:3539) at t.scheduleMicroTask (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:14:3791) at r.execute (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:166:4372) at queueRunnerFa ... at <Jasmine> ``` We now include the describe block description in the error, so that it is easier to figure out the location of the culprit code.
1 parent 72c2567 commit de86285

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

packages/zone.js/lib/jasmine/jasmine.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
3838
if (!ProxyZoneSpec) throw new Error('Missing: ProxyZoneSpec');
3939

4040
const ambientZone = Zone.current;
41-
// Create a synchronous-only zone in which to run `describe` blocks in order to raise an
42-
// error if any asynchronous operations are attempted inside of a `describe` but outside of
43-
// a `beforeEach` or `it`.
44-
const syncZone = ambientZone.fork(new SyncTestZoneSpec('jasmine.describe'));
4541

4642
const symbol = Zone.__symbol__;
4743

@@ -100,7 +96,8 @@ Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
10096
['describe', 'xdescribe', 'fdescribe'].forEach(methodName => {
10197
let originalJasmineFn: Function = jasmineEnv[methodName];
10298
jasmineEnv[methodName] = function(description: string, specDefinitions: Function) {
103-
return originalJasmineFn.call(this, description, wrapDescribeInZone(specDefinitions));
99+
return originalJasmineFn.call(
100+
this, description, wrapDescribeInZone(description, specDefinitions));
104101
};
105102
});
106103
['it', 'xit', 'fit'].forEach(methodName => {
@@ -198,8 +195,11 @@ Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
198195
* Gets a function wrapping the body of a Jasmine `describe` block to execute in a
199196
* synchronous-only zone.
200197
*/
201-
function wrapDescribeInZone(describeBody: Function): Function {
198+
function wrapDescribeInZone(description: string, describeBody: Function): Function {
202199
return function(this: unknown) {
200+
// Create a synchronous-only zone in which to run `describe` blocks in order to raise an
201+
// error if any asynchronous operations are attempted inside of a `describe`.
202+
const syncZone = ambientZone.fork(new SyncTestZoneSpec(`jasmine.describe#${description}`));
203203
return syncZone.run(describeBody, this, (arguments as any) as any[]);
204204
};
205205
}
@@ -332,6 +332,10 @@ Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
332332

333333
if (!isChildOfAmbientZone) throw new Error('Unexpected Zone: ' + Zone.current.name);
334334

335+
336+
Error.stackTraceLimit = Infinity;
337+
let context = new Error().stack;
338+
335339
// This is the zone which will be used for running individual tests.
336340
// It will be a proxy zone, so that the tests function can retroactively install
337341
// different zones.
@@ -345,6 +349,7 @@ Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
345349
this.testProxyZoneSpec = new ProxyZoneSpec();
346350
this.testProxyZone = ambientZone.fork(this.testProxyZoneSpec);
347351
if (!Zone.currentTask) {
352+
console.error('Scheduling', context, Zone.current.name)
348353
// if we are not running in a task then if someone would register a
349354
// element.addEventListener and then calling element.click() the
350355
// addEventListener callback would think that it is the top most task and would

packages/zone.js/test/jasmine-patch.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ifEnvSupports(supportJasmineSpec, () => {
3838

3939
it('should throw on async in describe', () => {
4040
expect(throwOnAsync).toBe(true);
41-
expect(syncZone.name).toEqual('syncTestZone for jasmine.describe');
41+
expect(syncZone.name).toEqual('syncTestZone for jasmine.describe#jasmine');
4242
itZone = Zone.current;
4343
});
4444

0 commit comments

Comments
 (0)