Skip to content

Commit

Permalink
feat: remove destroyAfterEach and teardown options (#1768)
Browse files Browse the repository at this point in the history
BREAKING CHANGE
`destroyAfterEach` and `teardown` are no longer available to use, please use `testEnvironmentOptions` instead.
  • Loading branch information
ahnpnl authored Sep 11, 2022
1 parent 22f2cd6 commit fe4c73b
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 155 deletions.
32 changes: 1 addition & 31 deletions setup-jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,6 @@ const {
platformBrowserDynamicTesting,
} = require('@angular/platform-browser-dynamic/testing');

let testEnvironmentOptions = globalThis.ngJest?.testEnvironmentOptions ?? Object.create(null);

const configuredDestroyAfterEach = globalThis.ngJest?.destroyAfterEach;
if (configuredDestroyAfterEach) {
console.warn(
'Passing destroyAfterEach for configuring the test environment has been deprecated.' +
' Please pass a `testEnvironmentOptions` object with TestEnvironmentOptions interface instead,' +
' see https://angular.io/api/core/testing/TestEnvironmentOptions'
);

testEnvironmentOptions = {
...testEnvironmentOptions,
teardown: {
destroyAfterEach: true,
},
};
}

const configuredTeardown = globalThis.ngJest?.teardown;
if (configuredTeardown) {
console.warn(
'Passing teardown for configuring the test environment has been deprecated.' +
' Please pass a `testEnvironmentOptions` object with TestEnvironmentOptions interface instead,' +
' see https://angular.io/api/core/testing/TestEnvironmentOptions'
);

testEnvironmentOptions = {
...testEnvironmentOptions,
teardown: configuredTeardown,
};
}
const testEnvironmentOptions = globalThis.ngJest?.testEnvironmentOptions ?? Object.create(null);

getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), testEnvironmentOptions);
32 changes: 1 addition & 31 deletions setup-jest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,6 @@ 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 testEnvironmentOptions = globalThis.ngJest?.testEnvironmentOptions ?? Object.create(null);

const configuredDestroyAfterEach = globalThis.ngJest?.destroyAfterEach;
if (configuredDestroyAfterEach) {
console.warn(
'Passing destroyAfterEach for configuring the test environment has been deprecated.' +
' Please pass a `testEnvironmentOptions` object with TestEnvironmentOptions interface instead,' +
' see https://angular.io/api/core/testing/TestEnvironmentOptions',
);

testEnvironmentOptions = {
...testEnvironmentOptions,
teardown: {
destroyAfterEach: true,
},
};
}

const configuredTeardown = globalThis.ngJest?.teardown;
if (configuredTeardown) {
console.warn(
'Passing teardown for configuring the test environment has been deprecated.' +
' Please pass a `testEnvironmentOptions` object with TestEnvironmentOptions interface instead,' +
' see https://angular.io/api/core/testing/TestEnvironmentOptions',
);

testEnvironmentOptions = {
...testEnvironmentOptions,
teardown: configuredTeardown,
};
}
const testEnvironmentOptions = globalThis.ngJest?.testEnvironmentOptions ?? Object.create(null);

getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), testEnvironmentOptions);
90 changes: 0 additions & 90 deletions src/config/setup-jest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,51 +52,6 @@ describe('setup-jest', () => {
});

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

expect(mockUmdZoneJs).toHaveBeenCalled();
expect(spyConsoleWarn).toHaveBeenCalled();
expect(spyConsoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Passing teardown for configuring the test environment has been deprecated. Please pass a \`testEnvironmentOptions\` object with TestEnvironmentOptions interface instead, see https://angular.io/api/core/testing/TestEnvironmentOptions"`,
);
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 \`testEnvironmentOptions\` object with TestEnvironmentOptions interface instead, see https://angular.io/api/core/testing/TestEnvironmentOptions"`,
);
assertOnInitTestEnv();
expect(mockInitTestEnvironment.mock.calls[0][2]).toEqual({
teardown: {
destroyAfterEach: true,
},
});
});

test('should call getTestBed() and initTestEnvironment() with the testEnvironmentOptions passed to ngJest', async () => {
globalThis.ngJest = {
testEnvironmentOptions: {
Expand Down Expand Up @@ -125,51 +80,6 @@ describe('setup-jest', () => {
});

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

expect(mockEsmZoneJs).toHaveBeenCalled();
expect(spyConsoleWarn).toHaveBeenCalled();
expect(spyConsoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Passing teardown for configuring the test environment has been deprecated. Please pass a \`testEnvironmentOptions\` object with TestEnvironmentOptions interface instead, see https://angular.io/api/core/testing/TestEnvironmentOptions"`,
);
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.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 \`testEnvironmentOptions\` object with TestEnvironmentOptions interface instead, see https://angular.io/api/core/testing/TestEnvironmentOptions"`,
);
assertOnInitTestEnv();
expect(mockInitTestEnvironment.mock.calls[0][2]).toEqual({
teardown: {
destroyAfterEach: true,
},
});
});

test('should call getTestBed() and initTestEnvironment() with the testEnvironmentOptions passed to ngJest', async () => {
globalThis.ngJest = {
testEnvironmentOptions: {
Expand Down
4 changes: 1 addition & 3 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable */

import type { ModuleTeardownOptions, TestEnvironmentOptions } from '@angular/core/testing';
import type { TestEnvironmentOptions } from '@angular/core/testing';

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

0 comments on commit fe4c73b

Please sign in to comment.