Skip to content

Commit

Permalink
fix: allow to just pass the destroyAfterEach flag
Browse files Browse the repository at this point in the history
  • Loading branch information
u120655 committed May 16, 2022
1 parent 1bd5d35 commit 99e4979
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
9 changes: 8 additions & 1 deletion setup-jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ const {
platformBrowserDynamicTesting,
} = require('@angular/platform-browser-dynamic/testing');

const teardown = globalThis.ngJest?.teardown;
let teardown = globalThis.ngJest?.teardown;
const configuredDestroyAfterEach = globalThis.ngJest?.destroyAfterEach;
if (configuredDestroyAfterEach) {
teardown = {
destroyAfterEach: true,
};
}

if (teardown !== undefined) {
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
teardown,
Expand Down
10 changes: 9 additions & 1 deletion setup-jest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import 'zone.js/fesm2015/zone-testing-bundle.min.js';
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

const teardown = globalThis.ngJest?.teardown;
let teardown = globalThis.ngJest?.teardown;
const configuredDestroyAfterEach = globalThis.ngJest?.destroyAfterEach;

if (configuredDestroyAfterEach) {
teardown = {
destroyAfterEach: true,
};
}

if (teardown !== undefined) {
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
teardown,
Expand Down
21 changes: 20 additions & 1 deletion src/config/setup-jest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,33 @@ describe('setup-jest', () => {
beforeEach(() => {
delete globalThis.ngJest;
jest.clearAllMocks();
jest.resetModules();
});

test('should initialize test environment with getTestBed() and initTestEnvironment() for CJS setup-jest', async () => {
globalThis.ngJest = {
teardown: {
destroyAfterEach: true,
destroyAfterEach: false,
rethrowErrors: true,
},
};
await import('../../setup-jest');

expect(mockUmdZoneJs).toHaveBeenCalled();
assertOnInitTestEnv();
expect(mockInitTestEnvironment.mock.calls[0][2]).toEqual({
teardown: {
destroyAfterEach: false,
rethrowErrors: true,
},
});
});

test('should initialize test environment with getTestBed() and initTestEnvironment() for CJS setup-jest / 2', async () => {
globalThis.ngJest = {
destroyAfterEach: true,
};

await import('../../setup-jest');

expect(mockUmdZoneJs).toHaveBeenCalled();
Expand Down

0 comments on commit 99e4979

Please sign in to comment.