5

I am getting the following error:

Cannot find module 'setupDevtools' from 'setup.js'

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:169:17

My JSON.package file looks like this:

{
  "name": "MiBase",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "babel-preset-env": "^1.7.0",
    "i": "^0.3.6",
    "jest-react-native": "^18.0.0",
    "react": "16.3.1",
    "react-native": "0.55.3",
    "react-native-datepicker": "^1.7.2",
    "react-native-image-picker": "^0.26.7",
    "react-native-ui-xg": "0.0.6",
    "react-navigation": "^1.5.11"
  },
  "devDependencies": {
    "babel-jest": "^22.4.3",
    "babel-preset-react-native": "3.0.0",
    "jest": "22.4.3",
    "react-test-renderer": "^16.3.1"
  },
  "jest": {
     "preset": "react-native",
     "moduleNameMapper": {
     "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__tests__/loginTest.js",
     "\\.(css|sass)$": "<rootDir>/__tests__/loginTest.js"
   },
   "transformIgnorePatterns": [
     "node_modules"
   ],
   "coveragePathIgnorePatterns": [
     "node_modules"
   ],
     "modulePathIgnorePatterns": [
     "node_modules"
   ]
 }

}

My test file looks like this:

// __tests__/loginTest.js
import 'react-native';
import React from 'react';
import HomeSreen from '../App';

import renderer from 'react-test-renderer';

test('renders correctly', () => {
  const tree = renderer.create(<LoginScreen />).toJSON();
  expect(tree).toMatchSnapshot();
});

I am very new to React Native and even newer to testing with it. Everything I have learnt or have tried to solve this issue has come from these three places:

https://facebook.github.io/jest/docs/en/tutorial-react-native.html Jest Cannot find module 'setupDevtools' from 'setup.js' Jest - Cannot find module 'setupDevtools' from 'setup.js'

I also have other functions in the class that I am not trying to test at this point. I am only trying to test the GUI functionality and that everything displays correctly. These functions are for processing http responses and JSON parsing.

skyboyer
  • 22,209
  • 7
  • 57
  • 64

2 Answers2

2

In my case I work with TypeScript and I had the same error and fix it with the following package.json configuration:

"dependencies": {
    "react": "^16.3.0-alpha.1",
    "react-native": "0.54.3"
},
"devDependencies": {
    "@types/jest": "^22.2.2",
    "@types/react": "^16.0.41",
    "@types/react-native": "^0.52.19",
    "@types/react-test-renderer": "^16.0.1",
    "babel-jest": "22.4.3",
    "babel-preset-react-native": "4.0.0",
    "jest": "22.4.3",
    "react-addons-test-utils": "^15.6.2",
    "react-native-mock": "^0.3.1",
    "react-native-typescript-transformer": "^1.2.3",
    "react-test-renderer": "^16.3.0-alpha.1",
    "ts-jest": "^22.4.2",
    "typescript": "^2.8.1"
},
"jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
        "ts",
        "tsx",
        "js"
    ],
    "transform": {
        "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
        "\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "testPathIgnorePatterns": [
        "\\.snap$",
        "<rootDir>/node_modules/",
        "<rootDir>/lib/"
    ],
    "cacheDirectory": ".jest/cache"
}

You can find this config on the this repo

I hope I've helped.

1

I solved this same issue with below configuration:

My jest / enzyme configuration:

// package.json
 "jest": {
    "preset": "react-native",
    "setupFiles": [
      "./config/enzyme.js"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 90,
        "functions": 90,
        "lines": 95,
        "statements": 96
      }
    },
    "moduleFileExtensions": [
      "js"
    ],
    "coverageDirectory": "coverage",
    "collectCoverageFrom": [
      "src/**/*.js"
    ]
  }

Jest setup file:

// config/enzyme.js
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

Example: @rimiti/react-native-toastify

Dimitri
  • 922
  • 2
  • 13
  • 34