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

Most upvoted comments

This also works for Error: Uncaught [TypeError: Cannot read property 'addListener' of undefined]:

Object.defineProperty(window, 'matchMedia', {
    value: () => {
      return {
        matches: false,
        addListener: () => {},
        removeListener: () => {}
      };
    }
})

This worked for me:

setupTests.ts

global.matchMedia = global.matchMedia || function () {
  return {
    matches: false,
    addListener: jest.fn(),
    removeListener: jest.fn(),
  };
};

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:

Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: jest.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: jest.fn(), // deprecated
    removeListener: jest.fn(), // deprecated
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    dispatchEvent: jest.fn(),
  })),
});

source

TypeError: Cannot read property 'addListener' of undefined Is what I got after Add the mocked matchMedia object

Hi,

I solved this by doing:

Adding in jest.config.js file:

...
setupFilesAfterEnv: ['./jest.setup.js']

Creating a file jest.setup.js in root folder with:

Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: jest.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: jest.fn(), // deprecated
    removeListener: jest.fn(), // deprecated
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    dispatchEvent: jest.fn()
  }))
})

ps: setupFilesAfterEnv is the “new” deprecated setupTestFrameworkScriptFile.

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:

Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: jest.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: jest.fn(), // deprecated
    removeListener: jest.fn(), // deprecated
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    dispatchEvent: jest.fn(),
  })),
});

source

Receiving the following error after using this mock object:

TypeError: mql.addListener is not a function

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:

Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: jest.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: jest.fn(), // deprecated
    removeListener: jest.fn(), // deprecated
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    dispatchEvent: jest.fn(),
  })),
});

source

Receiving the following error after using this mock object:

TypeError: mql.addListener is not a function

I have the same probelm. have you solved it?

@tambaqui thank you it fixed my problem, it should be part of antd docs 💯

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:

Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: jest.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: jest.fn(), // deprecated
    removeListener: jest.fn(), // deprecated
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    dispatchEvent: jest.fn(),
  })),
});

source

Receiving the following error after using this mock object:

TypeError: mql.addListener is not a function

I have the same probelm. have you solved it?

I fixed it by removing jest.resetAllMocks() in my afterEach()

@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

@tambaqui thank you it fixed my problem, it should be part of antd docs 100