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 configuring destroyAfterEach for setup test env #1469

Merged
merged 1 commit into from
May 14, 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
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module.exports = {
es6: true,
'jest/globals': true,
},
globals: {
globalThis: false,
},
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
Expand Down
12 changes: 10 additions & 2 deletions setup-jest.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
require('zone.js/bundles/zone-testing-bundle.umd');

const { getTestBed } = require('@angular/core/testing');
const {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} = require('@angular/platform-browser-dynamic/testing');

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

getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
const configuredDestroyAfterEach = globalThis.ngJest?.destroyAfterEach;
if (configuredDestroyAfterEach !== undefined) {
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
teardown: {
destroyAfterEach: configuredDestroyAfterEach,
},
});
} else {
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
}
10 changes: 10 additions & 0 deletions src/config/setup-jest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,30 @@ describe('setup-jest', () => {
};

beforeEach(() => {
delete globalThis.ngJest;
jest.clearAllMocks();
});

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

expect(mockUmdZoneJs).toHaveBeenCalled();
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');

expect(mockEsmZoneJs).toHaveBeenCalled();
assertOnInitTestEnv();
expect(mockInitTestEnvironment.mock.calls[0][2]).toBeUndefined();
});
});
3 changes: 2 additions & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ declare global {
var ngJest: {
skipNgcc?: boolean;
tsconfig?: string;
};
destroyAfterEach?: boolean;
} | undefined;
}

export {}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"module": "CommonJS",
"lib": ["esnext", "dom"],
"types": ["node", "jest"]
}
},
"files": ["src/global.d.ts"]
}
16 changes: 16 additions & 0 deletions website/docs/getting-started/test-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@ we also make sure Jest test methods run in Zone context. Then we initialize the

While `setup-jest.js` above is for running Jest with **CommonJS** mode, we also provide [`setup-jest.mjs`](https://github.com/thymikee/jest-preset-angular/blob/main/setup-jest.mjs)
to run with **ESM** mode.

### Configure test environment

When creating Angular test environment with `TestBed`, it is possible to specify the behavior of `teardown` via `globalThis` in the Jest setup file.
For example:

```ts
// setup-test.ts
globalThis.ngJest = {
destroyAfterEach: true,
};

import 'jest-preset-angular/setup-jest';
```

`jest-preset-angular` will look at `globalThis.ngJest` and pass the correct `destroyAfterEach` to `TestBed`.