Can't run tests with jest and enzyme

#1

Hi, I’m developing an app with expo, and I’m trying to write tests using jest and enzyme, but I can’t make it run my tests.

I get the following error when trying to run:

 FAIL  __tests__/src/components/Badge.test.js
  ● Test suite failed to run

    TypeError: window.addEventListener is not a function
      
      at node_modules/schedule/cjs/schedule.development.js:8:6008
      at Object.<anonymous> (node_modules/schedule/cjs/schedule.development.js:8:7057)
      at Object.<anonymous> (node_modules/schedule/index.js:1:223)
      at node_modules/react-dom/cjs/react-dom.development.js:8:215

I tried setting the preset to react-native, but then I get other errors when trying to test a component that depends on Expo.

My package.json is as follow:

{
  "name": "empty-project-template",
  "main": "node_modules/expo/AppEntry.js",
  "private": true,
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "eject": "expo eject",
    "test": "node_modules/.bin/jest"
  },
  "jest": {
    "preset": "jest-expo",
    "testMatch": [
      "**/?(*.)test.js?(x)"
    ],
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],
    "setupFiles": [
      "<rootDir>/setup.js"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!react-native|react-navigation|expo)/"
    ]
  },
  "dependencies": {
    "axios": "^0.18.0",
    "date-fns": "^1.29.0",
    "expo": "^30.0.0",
    "prop-types": "^15.6.2",
    "react": "16.3.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-30.0.0.tar.gz",
    "react-native-expo-image-cache": "^3.1.1",
    "react-native-modal-dropdown": "^0.6.2",
    "react-native-open-maps": "^0.3.1",
    "react-native-render-html": "^3.10.0",
    "react-native-responsive-screen": "^1.1.7",
    "react-native-status-bar-height": "^2.0.0",
    "react-navigation": "^2.13.0",
    "react-redux": "^5.0.7",
    "redux": "^4.0.0",
    "redux-logger": "^3.0.6",
    "redux-saga": "^0.16.0"
  },
  "devDependencies": {
    "babel-eslint": "^8.2.6",
    "enzyme": "^3.6.0",
    "enzyme-adapter-react-16": "^1.5.0",
    "enzyme-to-json": "^3.3.4",
    "eslint": "^5.4.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jest": "^21.21.0",
    "eslint-plugin-jsx-a11y": "^6.1.1",
    "eslint-plugin-react": "^7.11.1",
    "jest-expo": "^30.0.0",
    "prettier": "^1.14.2",
    "prettier-eslint": "^8.8.2",
    "react-dom": "^16.5.2"
  }
}

And my setup.js:

import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
configure({ adapter: new Adapter() });

I’ve ran out of ideas, and I couldn’t find any solution on my searches.

Any help is appreciated.

#2

I finally figured out what the problem was. It was silly, really. It was just a version mismatch between react-dom and react.

Thank u

2 Likes
#3

Glad you got it figured out @otaviogaiao. Thanks for posting the solution!

Cheers,

Adam

closed #4

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.