angular/packages/zone.js/test/jasmine-patch.spec.ts
Paul Gschwendtner de86285f2e 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.
2022-07-18 19:19:00 +02:00

90 lines
2.4 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ifEnvSupports} from './test-util';
function supportJasmineSpec() {
return jasmine && (jasmine as any)['Spec'];
}
(supportJasmineSpec as any).message = 'jasmine spec';
ifEnvSupports(supportJasmineSpec, () => {
beforeEach(() => {
// assert that each jasmine run has a task, so that drainMicrotask works properly.
expect(Zone.currentTask).toBeTruthy();
});
describe('jasmine', () => {
let throwOnAsync = false;
let beforeEachZone: Zone|null = null;
let beforeAllZone: Zone|null = null;
let itZone: Zone|null = null;
const syncZone = Zone.current;
try {
Zone.current.scheduleMicroTask('dontallow', (): any => null);
} catch (e) {
throwOnAsync = true;
}
beforeAll(() => beforeAllZone = Zone.current);
beforeEach(() => beforeEachZone = Zone.current);
it('should throw on async in describe', () => {
expect(throwOnAsync).toBe(true);
expect(syncZone.name).toEqual('syncTestZone for jasmine.describe#jasmine');
itZone = Zone.current;
});
it('should cope with pending tests, which have no test body');
afterEach(() => {
let zone = Zone.current;
expect(zone.name).toEqual('ProxyZone');
expect(beforeEachZone!.name).toEqual(zone.name);
expect(itZone).toBe(zone);
});
afterAll(() => {
let zone = Zone.current;
expect(zone.name).toEqual('ProxyZone');
expect(beforeAllZone!.name).toEqual(zone.name);
});
});
describe('return promise', () => {
let log: string[];
beforeEach(() => {
log = [];
});
it('should wait for promise to resolve', () => {
return new Promise<void>((res, _) => {
setTimeout(() => {
log.push('resolved');
res();
}, 100);
});
});
afterEach(() => {
expect(log).toEqual(['resolved']);
});
});
describe('jasmine.createSpyObj', () => {
it('createSpyObj with properties should be able to be retrieved from the spy', () => {
const spy = jasmine.createSpyObj('obj', ['someFunction'], {prop1: 'foo'});
expect(spy.prop1).toEqual('foo');
const desc: any = Object.getOwnPropertyDescriptor(spy, 'prop1');
expect(desc.enumerable).toBe(true);
expect(desc.configurable).toBe(true);
});
});
})();