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

Function runInTestZone

packages/zone.js/lib/zone-spec/async-test.ts:270–333  ·  view source on GitHub ↗
(
      fn: Function,
      context: any,
      applyArgs: unknown[] | undefined,
      finishCallback: Function,
      failCallback: Function,
    )

Source from the content-addressed store, hash-verified

268 };
269
270 function runInTestZone(
271 fn: Function,
272 context: any,
273 applyArgs: unknown[] | undefined,
274 finishCallback: Function,
275 failCallback: Function,
276 ) {
277 const currentZone = Zone.current;
278 const AsyncTestZoneSpec = (Zone as any)['AsyncTestZoneSpec'];
279 if (AsyncTestZoneSpec === undefined) {
280 throw new Error(
281 'AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
282 'Please make sure that your environment includes zone.js/plugins/async-test',
283 );
284 }
285 const ProxyZoneSpec = (Zone as any)['ProxyZoneSpec'] as {
286 get(): {setDelegate(spec: ZoneSpec): void; getDelegate(): ZoneSpec};
287 assertPresent: () => void;
288 };
289 if (!ProxyZoneSpec) {
290 throw new Error(
291 'ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
292 'Please make sure that your environment includes zone.js/plugins/proxy',
293 );
294 }
295 const proxyZoneSpec = ProxyZoneSpec.get();
296 ProxyZoneSpec.assertPresent();
297 // We need to create the AsyncTestZoneSpec outside the ProxyZone.
298 // If we do it in ProxyZone then we will get to infinite recursion.
299 const proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
300 const previousDelegate = proxyZoneSpec.getDelegate();
301 proxyZone!.parent!.run(() => {
302 const testZoneSpec: ZoneSpec = new AsyncTestZoneSpec(
303 () => {
304 // Need to restore the original zone.
305 if (proxyZoneSpec.getDelegate() == testZoneSpec) {
306 // Only reset the zone spec if it's
307 // still this one. Otherwise, assume
308 // it's OK.
309 proxyZoneSpec.setDelegate(previousDelegate);
310 }
311 (testZoneSpec as any).unPatchPromiseForTest();
312 currentZone.run(() => {
313 finishCallback();
314 });
315 },
316 (error: any) => {
317 // Need to restore the original zone.
318 if (proxyZoneSpec.getDelegate() == testZoneSpec) {
319 // Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
320 proxyZoneSpec.setDelegate(previousDelegate);
321 }
322 (testZoneSpec as any).unPatchPromiseForTest();
323 currentZone.run(() => {
324 failCallback(error);
325 });
326 },
327 'test',

Callers 1

patchAsyncTestFunction · 0.70

Calls 10

failCallbackFunction · 0.85
assertPresentMethod · 0.80
getDelegateMethod · 0.80
setDelegateMethod · 0.80
unPatchPromiseForTestMethod · 0.80
patchPromiseForTestMethod · 0.80
getMethod · 0.65
getZoneWithMethod · 0.65
runMethod · 0.65
runGuardedMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…