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

chore(jest): update default mocks #1371

Merged
merged 2 commits into from
May 24, 2023
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
1 change: 1 addition & 0 deletions MIGRATION.MD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

1. Jest 29 is required now, refer to its [CHANGELOG](https://github.com/kulshekhar/ts-jest/blob/main/CHANGELOG.md#2900-2022-09-08) for details.
2. `jest-preset-angular` has been updated to version 13. Make sure you understand the implications and perform all the necessary changes to your code base as described in `jest-preset-angular` [CHANGELOG](https://github.com/thymikee/jest-preset-angular/blob/main/CHANGELOG.md#1300-2023-02-18).
3. Global mocks for `getComputedStyle` and `doctype` are disabled by default, the `jsdom` implementation is used instead. The mocks are still available via the option `globalMocks`.

# Migration from version 14 to version 15

Expand Down
20 changes: 7 additions & 13 deletions examples/jest/simple-app/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@ import { AppComponent } from './app.component';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

describe('AppComponent', () => {
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
schemas: [
CUSTOM_ELEMENTS_SCHEMA
]
}).compileComponents();
})
);
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents();
}));

it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
});

it(`should have all the global mocks defined`, () => {
expect(window.getComputedStyle).toBeTruthy();
expect(document.doctype as any).toEqual('<!DOCTYPE html>');
it(`should have all the default global mocks are defined`, () => {
expect(window.matchMedia).toBeTruthy();
expect(document.body.style.transform).toBeTruthy();
});
Expand Down
10 changes: 5 additions & 5 deletions packages/jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,20 @@ The builder supports multi-project workspaces out of the box, the only thing req
Or, if you'd like the same custom configuration to be applied to all the projects in the workspace, you just specify it in _package.json_. Another option in such a case is creating a single config file in the workspace root and specifying it in _angular.json_ for each project.

- `tsConfig` - path to tsconfig file. If the path is relative then it is evaluated relative to the _project root_. Defaults to `tsconfig.spec.json` that is located in _project root_.
- `globalMocks` - there are 4 global mocks enabled by default:
- `globalMocks` - there are 4 global mocks available:
`["getComputedStyle", "doctype", "styleTransform", "matchMedia"]`.
The mocks implementation can be found [here](./src/global-mocks).

`styleTransform` and `matchMedia` are not implemented in jsdom.
`styleTransform` and `matchMedia` are not implemented in jsdom, therefore these are enabled by default.

`getComputedStyle` and `doctype` are implemented but do not support 100% of cases.
In the next major release, we'll still provide global mocks for implemented jsdom functions but they won't be enabled by default.

If you want to disable one or more of these mocks just pass an updated array in options.
If you want to disable/enable one or more of these mocks just pass an updated list in options.
For example:

```json
"options": {
"globalMocks": ["styleTransform", "matchMedia"]
"globalMocks": ["styleTransform", "matchMedia", "getComputedStyle"]
}
```

Expand Down
2 changes: 1 addition & 1 deletion packages/jest/src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"items": {
"type": "string"
},
"default": ["getComputedStyle", "doctype", "styleTransform", "matchMedia"]
"default": ["styleTransform", "matchMedia"]
},
"all": {
"description": "The opposite of `onlyChanged`. If `onlyChanged` is set by default, running jest with `--all` will force Jest to run all tests instead of running only tests related to changed files."
Expand Down