ant-design: 4.0.0-rc.1 TypeError: window.matchMedia is not a function in Jest
- I have searched the issues of this repository and believe that this is not a duplicate.
Reproduction link
http://not/required/see/description
Steps to reproduce
Using react-testing-tools along with an antd form, will result in this error:
TypeError: window.matchMedia is not a function
53 |
54 | const customRender = (ui: ReactElement<any>, options?: any) =>
> 55 | render(ui, { wrapper: providers(), ...options });
| ^
56 |
57 | const loggedInRender = (ui: ReactElement<any>, options?: any) =>
58 | render(ui, { wrapper: providers(loggedInStore), ...options });
at matchMedia (node_modules/antd/lib/_util/_util/responsiveObserve.js:58:32)
at Array.forEach (<anonymous>)
at Object.forEach [as register] (node_modules/antd/lib/_util/_util/responsiveObserve.js:53:36)
at Object.register [as subscribe] (node_modules/antd/lib/_util/_util/responsiveObserve.js:27:18)
at Row.subscribe [as componentDidMount] (node_modules/antd/lib/grid/grid/row.jsx:63:40)
at commitLifeCycles (node_modules/react-dom/cjs/react-dom.development.js:22101:22)
at commitLayoutEffects (node_modules/react-dom/cjs/react-dom.development.js:25344:7)
at HTMLUnknownElement.callCallback (node_modules/react-dom/cjs/react-dom.development.js:336:14)
at Object.invokeGuardedCallbackDev (node_modules/react-dom/cjs/react-dom.development.js:385:16)
at invokeGuardedCallback (node_modules/react-dom/cjs/react-dom.development.js:440:31)
at commitRootImpl (node_modules/react-dom/cjs/react-dom.development.js:25082:9)
at unstable_runWithPriority (node_modules/scheduler/cjs/scheduler.development.js:697:12)
at runWithPriority$2 (node_modules/react-dom/cjs/react-dom.development.js:12149:10)
at commitRoot (node_modules/react-dom/cjs/react-dom.development.js:24922:3)
at finishSyncRender (node_modules/react-dom/cjs/react-dom.development.js:24329:3)
at performSyncWorkOnRoot (node_modules/react-dom/cjs/react-dom.development.js:24307:9)
at scheduleUpdateOnFiber (node_modules/react-dom/cjs/react-dom.development.js:23698:7)
at updateContainer (node_modules/react-dom/cjs/react-dom.development.js:27103:3)
at node_modules/react-dom/cjs/react-dom.development.js:27528:7
at unbatchedUpdates (node_modules/react-dom/cjs/react-dom.development.js:24433:12)
at legacyRenderSubtreeIntoContainer (node_modules/react-dom/cjs/react-dom.development.js:27527:5)
at Object.render (node_modules/react-dom/cjs/react-dom.development.js:27608:10)
at node_modules/@testing-library/react/dist/pure.js:86:25
at batchedUpdates$1 (node_modules/react-dom/cjs/react-dom.development.js:24386:12)
at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:1092:14)
at render (node_modules/@testing-library/react/dist/pure.js:82:26)
at customRender (src/utils/test-utils.tsx:55:3)
at Object.<anonymous> (src/components/__tests__/SignUpForm.test.tsx:61:45)
What is expected?
No error to be returned.
What is actually happening?
Forms break jest and react-testing-tool tests.
Environment | Info |
---|---|
antd | 4.0.0-rc.2 |
React | 16.12.0 |
System | Manjaro Linux 5.4.12 |
Browser | 79.0.3945.117 |
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (3 by maintainers)
Commits related to this issue
- mock the window.matchMedia method, fixed failing tests likely introduced by ant designed 'Error: Uncaught [TypeError: window.matchMedia is not a function]' https://github.com/ant-design/ant-design/is... — committed to ademidun/atila-client-web-app by ademidun 4 years ago
- tests: fix test fault #63 https://github.com/ant-design/ant-design/issues/21096 — committed to ani-team/github-client by azinit 4 years ago
- change setupTests.js config see: https://github.com/ant-design/ant-design/issues/21096#issuecomment-725301551 and https://stackoverflow.com/a/64872224/5405197 — committed to ademidun/atila-client-web-app by ademidun 2 years ago
This also works for
Error: Uncaught [TypeError: Cannot read property 'addListener' of undefined]
:This worked for me:
setupTests.ts
More here #27658
You can fix this by mocking the window.matchMedia method in the setupTests.js file or before your tests. Here’s the mock I use:
source
TypeError: Cannot read property 'addListener' of undefined
Is what I got after Add the mocked matchMedia objectHi,
I solved this by doing:
Adding in
jest.config.js
file:Creating a file
jest.setup.js
in root folder with:ps: setupFilesAfterEnv is the “new” deprecated setupTestFrameworkScriptFile.
Receiving the following error after using this mock object:
I have the same probelm. have you solved it?
@tambaqui thank you it fixed my problem, it should be part of antd docs 💯
I fixed it by removing
jest.resetAllMocks()
in myafterEach()
@tambaqui Thank you so much. You saved me.
react: 17.0.1 antd: 4.8.0 typescript: 4.0.5
I’m using the same approach as mentioned above, but I’m getiing
Error: Uncaught [TypeError: Cannot read property 'addListener' of undefined]
.totally, quite similar solutions didn’t work out for the project I’m busy with and this one helped