Skip to content

Commit

Permalink
fix(core): test cleanup should not throw if Zone is not present
Browse files Browse the repository at this point in the history
The test hooks should not throw if applications choose not to load ZoneJS.

fixes #48198
  • Loading branch information
atscott committed Mar 28, 2024
1 parent 86a359b commit 2a37275
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/core/testing/src/fake_async.ts
Expand Up @@ -25,6 +25,12 @@ export function resetFakeAsyncZone(): void {
throw new Error(fakeAsyncTestModuleNotLoadedErrorMessage);
}

export function resetFakeAsyncZoneIfExists(): void {
if (fakeAsyncTestModule) {
fakeAsyncTestModule.resetFakeAsyncZone();
}
}

/**
* Wraps a function to be executed in the `fakeAsync` zone:
* - Microtasks are manually executed by calling `flushMicrotasks()`.
Expand Down
4 changes: 2 additions & 2 deletions packages/core/testing/src/test_hooks.ts
Expand Up @@ -12,7 +12,7 @@
* allows tests to be asynchronous by either returning a promise or using a 'done' parameter.
*/

import {resetFakeAsyncZone} from './fake_async';
import {resetFakeAsyncZoneIfExists} from './fake_async';
import {TestBedImpl} from './test_bed';

// Reset the test providers and the fake async zone before each test.
Expand All @@ -32,7 +32,7 @@ function getCleanupHook(expectedTeardownValue: boolean) {
const testBed = TestBedImpl.INSTANCE;
if (testBed.shouldTearDownTestingModule() === expectedTeardownValue) {
testBed.resetTestingModule();
resetFakeAsyncZone();
resetFakeAsyncZoneIfExists();
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/testing/src/testing.ts
Expand Up @@ -14,7 +14,7 @@

export * from './async';
export {ComponentFixture} from './component_fixture';
export * from './fake_async';
export {resetFakeAsyncZone, discardPeriodicTasks, fakeAsync, flush, flushMicrotasks, tick} from './fake_async';
export {TestBed, getTestBed, TestBedStatic, inject, InjectSetupWrapper, withModule} from './test_bed';
export {TestComponentRenderer, ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, TestModuleMetadata, TestEnvironmentOptions, ModuleTeardownOptions} from './test_bed_common';
export * from './test_hooks';
Expand Down

0 comments on commit 2a37275

Please sign in to comment.