akiran / react-slick

React carousel component
http://react-slick.neostack.com/
MIT License
11.78k stars 2.11k forks source link

Issue with Jest #2202

Open BenParr64 opened 1 year ago

BenParr64 commented 1 year ago

Issue occuring when trying to write jest tests

I have followed the docs and added the following to setupTests.json

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

I am still getting the following error:

matchMedia not present, legacy browsers require a polyfill

Package Json:

{
  "name": "kegthat-headless",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "test": "jest"
  },
  "dependencies": {
    "@apollo/client": "^3.7.2",
    "@types/node": "18.11.15",
    "@types/react": "18.0.26",
    "@types/react-dom": "18.0.9",
    "axios": "^0.21.1",
    "eslint": "8.29.0",
    "eslint-config-next": "13.0.6",
    "jest-environment-jsdom": "^29.3.1",
    "lodash": "^4.17.21",
    "next": "13.0.6",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-slick": "^0.29.0",
    "slick-carousel": "^1.8.1",
    "typescript": "4.9.4"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.20.2",
    "@babel/preset-typescript": "^7.18.6",
    "@testing-library/dom": "^8.19.0",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@types/react-slick": "^0.23.10",
    "autoprefixer": "^10.4.13",
    "babel-jest": "^29.3.1",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^29.3.1",
    "jest-cli": "^29.3.1",
    "polyfill": "^0.1.0",
    "postcss": "^8.4.20",
    "tailwindcss": "^3.2.4",
    "ts-node": "^10.9.1"
  }
}

I cannot add a test-setup.js as there is already a jest.config.ts in the project. Is there a fix for this?

Cheers

lalosh commented 1 year ago

include this in your test-setup.js


/**
 * fix: `matchMedia` not present, legacy browsers require a polyfill
 */
global.matchMedia = global.matchMedia || function() {
  return {
    matches: false,
    addListener: function() { },
    removeListener: function() { }
  }
}
saikiranravupalli commented 1 year ago

Hi @BenParr64 , I'm currently facing similar issue. Have you been able to resolve it, or are you still experiencing the issue?

If you found a solution or any workarounds, I would appreciate it if you could share your insights.

BenParr64 commented 1 year ago

Hey mate, I think I just mocked it but I can try and find the code and get back to you! Cheers

Sent from Samsung Mobile on O2 Sent from Outlook for Androidhttps://aka.ms/AAb9ysg


From: saikiranravupalli @.> Sent: Thursday, May 25, 2023 6:22:42 AM To: akiran/react-slick @.> Cc: Ben Parr @.>; Mention @.> Subject: Re: [akiran/react-slick] Issue with Jest (Issue #2202)

Hi @BenParr64https://github.com/BenParr64 , I'm currently facing similar issue. Have you been able to resolve it, or are you still experiencing the issue?

If you found a solution or any workarounds, I would appreciate it if you could share your insights.

— Reply to this email directly, view it on GitHubhttps://github.com/akiran/react-slick/issues/2202#issuecomment-1562288470, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASYAXNNNKG5LSJJ6RNOZ5ZDXH3UCFANCNFSM6AAAAAATAAHQBE. You are receiving this because you were mentioned.Message ID: @.***>

ShotSkydiver commented 1 year ago

@saikiranravupalli You should also be able to fix it by adding this code near the top of the file containing your tests, before the tests are run:

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