MCPcopy Index your code
hub / github.com/angular/angular / patchJest

Function patchJest

packages/zone.js/lib/jest/jest.ts:14–315  ·  view source on GitHub ↗
(Zone: ZoneType)

Source from the content-addressed store, hash-verified

12declare let jest: any;
13
14export function patchJest(Zone: ZoneType): void {
15 Zone.__load_patch('jest', (context: any, Zone: ZoneType, api: _ZonePrivate) => {
16 if (typeof jest === 'undefined' || jest['__zone_patch__']) {
17 return;
18 }
19
20 // From jest 29 and jest-preset-angular v13, the module transform logic
21 // changed, and now jest-preset-angular use the use the tsconfig target
22 // other than the hardcoded one, https://github.com/thymikee/jest-preset-angular/issues/2010
23 // But jest-angular-preset doesn't introduce the @babel/plugin-transform-async-to-generator
24 // which is needed by angular since `async/await` still need to be transformed
25 // to promise for ES2017+ target.
26 // So for now, we disable to output the uncaught error console log for a temp solution,
27 // until jest-preset-angular find a proper solution.
28 (Zone as any)[api.symbol('ignoreConsoleErrorUncaughtError')] = true;
29 jest['__zone_patch__'] = true;
30
31 const ProxyZoneSpec = (Zone as any)['ProxyZoneSpec'];
32 const SyncTestZoneSpec = (Zone as any)['SyncTestZoneSpec'];
33
34 if (!ProxyZoneSpec) {
35 throw new Error('Missing ProxyZoneSpec');
36 }
37
38 const rootZone = Zone.current;
39 const syncZone = rootZone.fork(new SyncTestZoneSpec('jest.describe'));
40 const proxyZoneSpec = new ProxyZoneSpec();
41 const proxyZone = rootZone.fork(proxyZoneSpec);
42
43 function wrapDescribeFactoryInZone(originalJestFn: Function) {
44 return function (this: unknown, ...tableArgs: any[]) {
45 const originalDescribeFn = originalJestFn.apply(this, tableArgs);
46 return function (this: unknown, ...args: any[]) {
47 args[1] = wrapDescribeInZone(args[1]);
48 return originalDescribeFn.apply(this, args);
49 };
50 };
51 }
52
53 function wrapTestFactoryInZone(originalJestFn: Function) {
54 return function (this: unknown, ...tableArgs: any[]) {
55 return function (this: unknown, ...args: any[]) {
56 args[1] = wrapTestInZone(args[1]);
57 return originalJestFn.apply(this, tableArgs).apply(this, args);
58 };
59 };
60 }
61
62 /**
63 * Gets a function wrapping the body of a jest `describe` block to execute in a
64 * synchronous-only zone.
65 */
66 function wrapDescribeInZone(describeBody: Function): Function {
67 return function (this: unknown, ...args: any[]) {
68 return syncZone.run(describeBody, this, args);
69 };
70 }
71

Callers 1

rollupTestingFunction · 0.90

Calls 15

wrapTestFactoryInZoneFunction · 0.85
isPatchingFakeTimerFunction · 0.85
isInTestFuncFunction · 0.85
__symbol__Method · 0.80
flushMicrotasksMethod · 0.80
removeAllTimersMethod · 0.80
wrapDescribeInZoneFunction · 0.70
wrapTestInZoneFunction · 0.70
__load_patchMethod · 0.65
symbolMethod · 0.65
forkMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…