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

Jest testing with react: matchMedia not present, legacy browsers require a polyfill #742

Closed
janjuafarooq opened this issue May 3, 2017 · 14 comments

Comments

@janjuafarooq
Copy link

janjuafarooq commented May 3, 2017

Trying to shallow render a component using Enzyme and I see this error on my component. I see that this issue was "fixed" previously but I am still seeing it on "react-slick": "^0.14.11". Any suggestions to fix?

matchMedia not present, legacy browsers require a polyfill
at new MediaQueryDispatch (node_modules/enquire.js/src/MediaQueryDispatch.js:15:15)
at Object. (node_modules/enquire.js/src/index.js:2:18)
at Object. (node_modules/react-slick/lib/slider.js:37:38)
at Object. (node_modules/react-slick/lib/index.js:3:18)

I am using https://github.com/facebookincubator/create-react-app as my boilerplate

@andrewdc92
Copy link

did you already add this? https://github.com/akiran/react-slick#test-setup

@janjuafarooq
Copy link
Author

How can I incorporate this fix for the create-react-app starter kit?

https://github.com/facebookincubator/create-react-app

I don't have access to a jest command.

@janjuafarooq
Copy link
Author

janjuafarooq commented May 3, 2017

I had to create a file setupTests.js and place it inside the src/ folder (src/setupTests.js) and put the following code in:

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

After this I ran npm test again and it resolved my issue (had to stop the watch task and restart it).

This resolved my issue. Might want to update instructions for create-react-app

@akiran akiran closed this as completed May 12, 2017
@Slebee
Copy link

Slebee commented Jun 15, 2017

if you was eject your create-react-app,put that code in /config/polyfills.js

@astroash
Copy link

astroash commented Oct 4, 2017

@janjuafarooq did you manage to get this working without ejecting your create-react-app?

@vTrip
Copy link

vTrip commented Apr 18, 2018

@janjuafarooq could you explain why that fix works please :)

@mustkem
Copy link

mustkem commented Aug 14, 2018

Press ctrl + e in visual code. And search pollyfills.js file.
Open it and put this code.

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

You dont need to eject. Polyfills.js file will rescue in this case.

@igorfelipee
Copy link

If you are use react, you can just import an match-media-pollyfil in you App.test.js.

@leotm
Copy link

leotm commented Feb 4, 2019

Here are the official docs: Mocking methods which are not implemented in JSDOM

window.matchMedia = jest.fn().mockImplementation(query => {
  return {
    matches: false,
    media: query,
    onchange: null,
    addListener: jest.fn(),
    removeListener: jest.fn(),
  };
});

As a seperate file, then imported then included before the test file:

import './matchMedia.mock'; // Must be imported before the tested file
import {myMethod} from './file-to-test';

describe('myMethod()', () => {
  // Test the method here...
});

@Miltonjacomini
Copy link

For me fix was almost the same.

Include the file setupTests.js in src folder.

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

But i have include the insert file on Jest call in package.json.
"jest": { //... "setupFilesAfterEnv": [ "<rootDir>/src/setupTests.js" ]

and then run npm test again ... 👍

@zernabhussain
Copy link

add this piece of code:

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

in jest.setup.js or setupTests.js (you can check the name from jest.config.js
eg. setupFiles: ["./jest.setup.js"] )

@ioxua
Copy link

ioxua commented Jul 11, 2022

For anyone still having this issue: if you're using react-scripts@4.0.3, this may help:

Source

// $ npm i -D mq-polyfill
// in src/setupTests.ts

import matchMediaPolyfill from 'mq-polyfill'

matchMediaPolyfill(window)

window.resizeTo = function resizeTo(width, height) {
  Object.assign(this, {
    innerWidth: width,
    innerHeight: height,
    outerWidth: width,
    outerHeight: height
  }).dispatchEvent(new this.Event('resize'))
}

@KentuckySato
Copy link

@ioxua Thank you! It works for me!
My version of react-script is react-scripts": "^5.0.1 and your solution has solved my problem 🥳

@sbland
Copy link

sbland commented Feb 8, 2024

The fixes above didn't help for me so I just mocked react-slick in my setupTests.js file.

jest.mock("react-slick", () => ({
  __esModule: true,
  default: ({ children }) => <div data-testid="slick_mock">{children}</div>,
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests