Skip to content
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

feat: allow passing the whole ModuleTeardownOptions #1475

Merged
merged 3 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
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
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) {
chabb marked this conversation as resolved.
Show resolved Hide resolved
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;
chabb marked this conversation as resolved.
Show resolved Hide resolved
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`.