Skip to content

Commit

Permalink
feat: allow passing the teardown object to global-setup (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
chabb authored May 17, 2022
1 parent 8681882 commit 3dab02b
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 24 deletions.
18 changes: 14 additions & 4 deletions setup-jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@ const {
platformBrowserDynamicTesting,
} = require('@angular/platform-browser-dynamic/testing');

let teardown = globalThis.ngJest?.teardown;
const configuredDestroyAfterEach = globalThis.ngJest?.destroyAfterEach;
if (configuredDestroyAfterEach !== undefined) {
if (configuredDestroyAfterEach) {
console.warn(
'Passing destroyAfterEach for configuring the test environment has been deprecated.' +
' Please pass a `teardown` object with ModuleTeardownOptions interface instead,' +
' see https://github.com/angular/angular/blob/6952a0a3e68481564b2bc4955afb3ac186df6e34/packages/core/testing/src/test_bed_common.ts#L98'
);
teardown = {
destroyAfterEach: true,
};
}

if (teardown !== undefined) {
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
teardown: {
destroyAfterEach: configuredDestroyAfterEach,
},
teardown,
});
} else {
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
Expand Down
17 changes: 13 additions & 4 deletions setup-jest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ import 'zone.js/fesm2015/zone-testing-bundle.min.js';
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

let teardown = globalThis.ngJest?.teardown;
const configuredDestroyAfterEach = globalThis.ngJest?.destroyAfterEach;
if (configuredDestroyAfterEach !== undefined) {

if (configuredDestroyAfterEach) {
console.warn('Passing destroyAfterEach for configuring the test environment has been deprecated.' +
' Please pass a `teardown` object with ModuleTeardownOptions interface instead,' +
' see https://github.com/angular/angular/blob/6952a0a3e68481564b2bc4955afb3ac186df6e34/packages/core/testing/src/test_bed_common.ts#L98');
teardown = {
destroyAfterEach: true,
};
}

if (teardown !== undefined) {
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
teardown: {
destroyAfterEach: configuredDestroyAfterEach,
},
teardown,
});
} else {
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
Expand Down
92 changes: 77 additions & 15 deletions src/config/setup-jest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,90 @@ 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 = {
destroyAfterEach: true,
};
await import('../../setup-jest');
describe('for CJS setup-jest, test environment initialization', () => {
test('should call getTestBed() and initTestEnvironment() with the ModuleTeardownOptions object passed to ngJest', async () => {
globalThis.ngJest = {
teardown: {
destroyAfterEach: false,
rethrowErrors: true,
},
};
await import('../../setup-jest');

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

test('should call getTestBed() and initTestEnvironment() with the destroyAfterEach passed to ngJest', async () => {
const spyConsoleWarn = (console.warn = jest.fn());
globalThis.ngJest = {
destroyAfterEach: true,
},
};

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

expect(mockUmdZoneJs).toHaveBeenCalled();
expect(spyConsoleWarn).toHaveBeenCalled();
expect(spyConsoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Passing destroyAfterEach for configuring the test environment has been deprecated. Please pass a \`teardown\` object with ModuleTeardownOptions interface instead, see https://github.com/angular/angular/blob/6952a0a3e68481564b2bc4955afb3ac186df6e34/packages/core/testing/src/test_bed_common.ts#L98"`,
);
assertOnInitTestEnv();
expect(mockInitTestEnvironment.mock.calls[0][2]).toEqual({
teardown: {
destroyAfterEach: true,
},
});
});
});

test('should initialize test environment with getTestBed() and initTestEnvironment() for ESM setup-jest', async () => {
await import('../../setup-jest.mjs');
describe('for ESM setup-jest, test environment initialization', () => {
test('should call getTestBed() and initTestEnvironment() with the ModuleTeardownOptions object passed to ngJest', async () => {
globalThis.ngJest = {
teardown: {
destroyAfterEach: false,
rethrowErrors: true,
},
};
await import('../../setup-jest.mjs');

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

test('should call getTestBed() and initTestEnvironment() with the destroyAfterEach passed to ngJest', async () => {
const spyConsoleWarn = (console.warn = jest.fn());
globalThis.ngJest = {
destroyAfterEach: true,
};

expect(mockEsmZoneJs).toHaveBeenCalled();
assertOnInitTestEnv();
expect(mockInitTestEnvironment.mock.calls[0][2]).toBeUndefined();
await import('../../setup-jest.mjs');

expect(mockEsmZoneJs).toHaveBeenCalled();
expect(spyConsoleWarn).toHaveBeenCalled();
expect(spyConsoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Passing destroyAfterEach for configuring the test environment has been deprecated. Please pass a \`teardown\` object with ModuleTeardownOptions interface instead, see https://github.com/angular/angular/blob/6952a0a3e68481564b2bc4955afb3ac186df6e34/packages/core/testing/src/test_bed_common.ts#L98"`,
);
assertOnInitTestEnv();
expect(mockInitTestEnvironment.mock.calls[0][2]).toEqual({
teardown: {
destroyAfterEach: true,
},
});
});
});
});
3 changes: 3 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint-disable */

import type { ModuleTeardownOptions } from "@angular/core/testing";

declare global {
var ngJest: {
skipNgcc?: boolean;
tsconfig?: string;
destroyAfterEach?: boolean;
teardown?: ModuleTeardownOptions;
} | undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/getting-started/test-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ globalThis.ngJest = {
import 'jest-preset-angular/setup-jest';
```

`jest-preset-angular` will look at `globalThis.ngJest` and pass the correct `destroyAfterEach` to `TestBed`.
`jest-preset-angular` will look at `globalThis.ngJest` and pass the correct [`ModuleTearDownOptions`](https://github.com/angular/angular/blob/6952a0a3e68481564b2bc4955afb3ac186df6e34/packages/core/testing/src/test_bed_common.ts#L98) object to `TestBed`.

0 comments on commit 3dab02b

Please sign in to comment.