Skip to content

Commit

Permalink
feat(zone.js): include jasmine describe block name when raising une…
Browse files Browse the repository at this point in the history
…xpected 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.
  • Loading branch information
devversion committed Jul 18, 2022
1 parent bd61dc3 commit 08f31a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions packages/zone.js/lib/jasmine/jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
if (!ProxyZoneSpec) throw new Error('Missing: ProxyZoneSpec');

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

const symbol = Zone.__symbol__;

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

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


Error.stackTraceLimit = Infinity;
let context = new Error().stack;

// This is the zone which will be used for running individual tests.
// It will be a proxy zone, so that the tests function can retroactively install
// different zones.
Expand Down
2 changes: 1 addition & 1 deletion packages/zone.js/test/jasmine-patch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ifEnvSupports(supportJasmineSpec, () => {

it('should throw on async in describe', () => {
expect(throwOnAsync).toBe(true);
expect(syncZone.name).toEqual('syncTestZone for jasmine.describe');
expect(syncZone.name).toEqual('syncTestZone for jasmine.describe#jasmine');
itZone = Zone.current;
});

Expand Down

0 comments on commit 08f31a5

Please sign in to comment.