8000 fix(core): fakeAsync should not depend on module import order by atscott · Pull Request #61375 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

fix(core): fakeAsync should not depend on module import order #61375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 19 additions & 29 deletions packages/core/testing/src/fake_async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@
import type {} from 'zone.js';

const _Zone: any = typeof Zone !== 'undefined' ? Zone : null;
const fakeAsyncTestModule = _Zone && _Zone[_Zone.__symbol__('fakeAsyncTest')];
function getFakeAsyncTestModule() {
return _Zone && _Zone[_Zone.__symbol__('fakeAsyncTest')];
}

const fakeAsyncTestModuleNotLoadedErrorMessage = `zone-testing.js is needed for the fakeAsync() test helper but could not be found.
Please make sure that your environment includes zone.js/testing`;
function withFakeAsyncTestModule(fn: (fakeAsyncTestModule: any) => any): any {
const fakeAsyncTestModule = getFakeAsyncTestModule();
if (!fakeAsyncTestModule) {
throw new Error(`zone-testing.js is needed for the fakeAsync() test helper but could not be found.
Please make sure that your environment includes zone.js/testing`);
}
return fn(fakeAsyncTestModule);
}

/**
* Clears out the shared fake async zone for a test.
Expand All @@ -22,15 +30,12 @@ const fakeAsyncTestModuleNotLoadedErrorMessage = `zone-testing.js is needed for
* @publicApi
*/
export function resetFakeAsyncZone(): void {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.resetFakeAsyncZone();
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
withFakeAsyncTestModule((v) => v.resetFakeAsyncZone());
}

export function resetFakeAsyncZoneIfExists(): void {
if (fakeAsyncTestModule && (Zone as any)['ProxyZoneSpec']?.isLoaded()) {
fakeAsyncTestModule.resetFakeAsyncZone();
if (getFakeAsyncTestModule() && (Zone as any)['ProxyZoneSpec']?.isLoaded()) {
getFakeAsyncTestModule().resetFakeAsyncZone();
}
}

Expand Down Expand Up @@ -59,10 +64,7 @@ export function resetFakeAsyncZoneIfExists(): void {
* @publicApi
*/
export function fakeAsync(fn: Function, options?: {flush?: boolean}): (...args: any[]) => any {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.fakeAsync(fn, options);
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
return withFakeAsyncTestModule((v) => v.fakeAsync(fn, options));
}

/**
Expand Down Expand Up @@ -135,10 +137,7 @@ export function tick(
processNewMacroTasksSynchronously: true,
},
): void {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.tick(millis, tickOptions);
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
return withFakeAsyncTestModule((m) => m.tick(millis, tickOptions));
}

/**
Expand All @@ -153,10 +152,7 @@ export function tick(
* @publicApi
*/
export function flush(maxTurns?: number): number {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.flush(maxTurns);
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
return withFakeAsyncTestModule((m) => m.flush(maxTurns));
}

/**
Expand All @@ -165,10 +161,7 @@ export function flush(maxTurns?: number): number {
* @publicApi
*/
export function discardPeriodicTasks(): void {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.discardPeriodicTasks();
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
return withFakeAsyncTestModule((m) => m.discardPeriodicTasks());
}

/**
Expand All @@ -177,8 +170,5 @@ export function discardPeriodicTasks(): void {
* @publicApi
*/
export function flushMicrotasks(): void {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.flushMicrotasks();
}
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
return withFakeAsyncTestModule((m) => m.flushMicrotasks());
}
0