Skip to content

Commit ced2fa5

Browse files
committed
refactor(zone.js): Improve missing proxy zone error for jest imported (#64497)
test functions This improves the fakeAsync error message when importing it, describe, etc from jest We will not be further expanding the ZoneJS patches to support additional use-cases. fixes #47603 PR Close #64497
1 parent f0debd5 commit ced2fa5

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages/zone.js/lib/zone-spec/fake-async-test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {ZoneType} from '../zone-impl';
10-
import {type ProxyZoneSpec} from './proxy';
10+
import {throwProxyZoneError, type ProxyZoneSpec} from './proxy';
1111

1212
const global: any =
1313
(typeof window === 'object' && window) || (typeof self === 'object' && self) || globalThis.global;
@@ -954,10 +954,7 @@ export function fakeAsync(fn: Function, options: {flush?: boolean} = {}): (...ar
954954
const fakeAsyncFn: any = function (this: unknown, ...args: any[]) {
955955
const ProxyZoneSpec = getProxyZoneSpec();
956956
if (!ProxyZoneSpec) {
957-
throw new Error(
958-
'ProxyZoneSpec is needed for the fakeAsync() test helper but could not be found. ' +
959-
'Make sure that your environment includes zone-testing.js',
960-
);
957+
throwProxyZoneError();
961958
}
962959
const proxyZoneSpec = ProxyZoneSpec.assertPresent();
963960
if (Zone.current.get('FakeAsyncTestZoneSpec')) {

packages/zone.js/lib/zone-spec/proxy.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88

99
import {ZoneType} from '../zone-impl';
1010

11+
declare let jest: any;
12+
13+
export function throwProxyZoneError(): never {
14+
const jestPatched = typeof jest !== 'undefined' && jest['__zone_patch__'];
15+
if (jestPatched) {
16+
throw new Error(
17+
'Only globals are patched with zone-testing. If you import `it`, `describe`, etc. directly, you cannot use `fakeAsync` or `waitForAsync`.',
18+
);
19+
} else {
20+
throw new Error(
21+
'ProxyZoneSpec is needed for the fakeAsync and waitForAsync test helpers but could not be found. ' +
22+
'Make sure that your environment includes zone-testing.js',
23+
);
24+
}
25+
}
26+
1127
export class ProxyZoneSpec implements ZoneSpec {
1228
name: string = 'ProxyZone';
1329

0 commit comments

Comments
 (0)