Skip to content

Commit a2fcda6

Browse files
vvanpoSimenB
authored andcommittedDec 24, 2019
docs: Use Object.defineProperty() for stubbing global propert… (#9288)
1 parent acb9c09 commit a2fcda6

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed
 

‎docs/ManualMocks.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ If some code uses a method which JSDOM (the DOM implementation used by Jest) has
141141
In this case, mocking `matchMedia` in the test file should solve the issue:
142142

143143
```js
144-
window.matchMedia = jest.fn().mockImplementation(query => {
145-
return {
144+
Object.defineProperty(window, 'matchMedia', {
145+
writable: true,
146+
value: jest.fn().mockImplementation(query => ({
146147
matches: false,
147148
media: query,
148149
onchange: null,
@@ -151,7 +152,7 @@ window.matchMedia = jest.fn().mockImplementation(query => {
151152
addEventListener: jest.fn(),
152153
removeEventListener: jest.fn(),
153154
dispatchEvent: jest.fn(),
154-
};
155+
})),
155156
});
156157
```
157158

‎website/versioned_docs/version-23.x/ManualMocks.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ If some code uses a method which JSDOM (the DOM implementation used by Jest) has
142142
In this case, mocking `matchMedia` in the test file should solve the issue:
143143

144144
```js
145-
window.matchMedia = jest.fn().mockImplementation(query => {
146-
return {
145+
Object.defineProperty(window, 'matchMedia', {
146+
writable: true,
147+
value: jest.fn().mockImplementation(query => ({
147148
matches: false,
148149
media: query,
149150
onchange: null,
@@ -152,7 +153,7 @@ window.matchMedia = jest.fn().mockImplementation(query => {
152153
addEventListener: jest.fn(),
153154
removeEventListener: jest.fn(),
154155
dispatchEvent: jest.fn(),
155-
};
156+
})),
156157
});
157158
```
158159

0 commit comments

Comments
 (0)
Please sign in to comment.