Skip to content

Commit

Permalink
refactor: import zone.js/testing instead of zone-testing-bundle (#2163)
Browse files Browse the repository at this point in the history
Close #2162

From zone.js 0.13.2, the original
`zone.js/bundles/zone-testing-bundle.js` is not easily import and will
eventually removed from zone.js 0.14.0.

The proper way to import zone.js and zone.js/testing is

```
import 'zone.js';
import 'zone.js/testing';
```

So this PR use this way to import zone.js related bundles.
  • Loading branch information
JiaLiPassion authored Sep 13, 2023
1 parent 65a848a commit e47ae67
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
3 changes: 2 additions & 1 deletion setup-jest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('zone.js/bundles/zone-testing-bundle.umd');
require('zone.js');
require('zone.js/testing');
const { getTestBed } = require('@angular/core/testing');
const {
BrowserDynamicTestingModule,
Expand Down
3 changes: 2 additions & 1 deletion setup-jest.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'zone.js/fesm2015/zone-testing-bundle.min.js';
import 'zone.js';
import 'zone.js/testing';
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

Expand Down
18 changes: 10 additions & 8 deletions src/config/setup-jest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const mockInitTestEnvironment = jest.fn();
const mockUmdZoneJs = jest.fn();
const mockEsmZoneJs = jest.fn();
const mockZoneJs = jest.fn();
const mockZoneJsTesting = jest.fn();
const mockGetTestBed = jest.fn(() => {
return {
initTestEnvironment: mockInitTestEnvironment,
};
});
jest.mock('zone.js/bundles/zone-testing-bundle.umd', () => {
const mockedResult = mockUmdZoneJs();
jest.mock('zone.js', () => {
const mockedResult = mockZoneJs();

return {
mockedResult,
};
});
jest.mock('zone.js/fesm2015/zone-testing-bundle.min.js', () => {
const mockedResult = mockEsmZoneJs();
jest.mock('zone.js/testing', () => {
const mockedResult = mockZoneJsTesting();

return {
mockedResult,
Expand Down Expand Up @@ -66,7 +66,8 @@ describe('setup-jest', () => {

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

expect(mockUmdZoneJs).toHaveBeenCalled();
expect(mockZoneJs).toHaveBeenCalled();
expect(mockZoneJsTesting).toHaveBeenCalled();
assertOnInitTestEnv();
expect(mockInitTestEnvironment.mock.calls[0][2]).toEqual({
teardown: {
Expand Down Expand Up @@ -94,7 +95,8 @@ describe('setup-jest', () => {

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

expect(mockEsmZoneJs).toHaveBeenCalled();
expect(mockZoneJs).toHaveBeenCalled();
expect(mockZoneJsTesting).toHaveBeenCalled();
assertOnInitTestEnv();
expect(mockInitTestEnvironment.mock.calls[0][2]).toEqual({
teardown: {
Expand Down
3 changes: 2 additions & 1 deletion website/versioned_docs/version-11.0/guides/angular-13+.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ The `jest-preset-angular/setup-jest` file doesn't work with ESM, because it uses

```ts
// setup-jest.ts
import 'zone.js/fesm2015/zone-testing-bundle.min.js';
import 'zone.js';
import 'zone.js/testing';
import './jest-global-mocks';

import { getTestBed } from '@angular/core/testing';
Expand Down
3 changes: 2 additions & 1 deletion website/versioned_docs/version-11.1/guides/angular-13+.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ The `jest-preset-angular/setup-jest` file doesn't work with ESM, because it uses

```ts
// setup-jest.ts
import 'zone.js/fesm2015/zone-testing-bundle.min.js';
import 'zone.js';
import 'zone.js/testing';
import './jest-global-mocks';

import { getTestBed } from '@angular/core/testing';
Expand Down

0 comments on commit e47ae67

Please sign in to comment.