Skip to content

Commit

Permalink
feat: allow configuring destroyAfterEach for setup test env (#1469)
Browse files Browse the repository at this point in the history
Closes #1466
  • Loading branch information
ahnpnl authored May 14, 2022
1 parent b4e3b6d commit 9a735f9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
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`.

0 comments on commit 9a735f9

Please sign in to comment.