Skip to content

Commit

Permalink
docs: Use Object.defineProperty() for stubbing global propert… (#9288)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvanpo authored and SimenB committed Dec 24, 2019
1 parent acb9c09 commit a2fcda6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions docs/ManualMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ If some code uses a method which JSDOM (the DOM implementation used by Jest) has
In this case, mocking `matchMedia` in the test file should solve the issue:

```js
window.matchMedia = jest.fn().mockImplementation(query => {
return {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
Expand All @@ -151,7 +152,7 @@ window.matchMedia = jest.fn().mockImplementation(query => {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
};
})),
});
```

Expand Down
7 changes: 4 additions & 3 deletions website/versioned_docs/version-23.x/ManualMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ If some code uses a method which JSDOM (the DOM implementation used by Jest) has
In this case, mocking `matchMedia` in the test file should solve the issue:

```js
window.matchMedia = jest.fn().mockImplementation(query => {
return {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
Expand All @@ -152,7 +153,7 @@ window.matchMedia = jest.fn().mockImplementation(query => {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
};
})),
});
```

Expand Down

0 comments on commit a2fcda6

Please sign in to comment.