jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.12k stars 6.44k forks source link

Cannot find module 'setupDevtools' from 'setup.js' - Platform Windows #3822

Closed meinto closed 7 years ago

meinto commented 7 years ago

Do you want to request a feature or report a bug?

bug

What is the current behavior?

When i run the tests on a linux maschine everything is fine. When i run the tests with the same configuration on my windows computer the following error is thrown:

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

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

OS - Windows

package.json

{
  ...
  "dependencies": {
    "react": "16.0.0-alpha.12",
    "react-native": "0.45.1",
    "react-native-mirror": "0.0.19"
  },
  "devDependencies": {
    "babel-jest": "20.0.3",
    "babel-preset-react-native": "1.9.2",
    "cross-env": "^5.0.0",
    "jasmine-reporters": "2.2.1",
    "jest-cli": "20.0.4",
    "react-native-cli": "^2.0.1",
    "react-test-renderer": "16.0.0-alpha.13"
  },
  "jest": {
    "collectCoverage": true,
    "setupTestFrameworkScriptFile": "./setup-jasmine-env.js",
    "preset": "react-native"
  }
}

setup-jasmine-env.js

var jasmineReporters = require('jasmine-reporters')
jasmine.VERBOSE = true

.babelrc

{
  "presets": [
    "react-native"
  ],
  "retainLines": true,
  "sourceMaps": true
}

Many thanks for helping!

tuanmai commented 7 years ago

@tobiasMeinhardt Hey, we encountered the same issue with you. Our solution is rm yarn.lock && rm -rf node_modules && yarn. Seems that there is a problem with the yarn.lock file

cpojer commented 7 years ago

I think that was some dependency that I published either for Jest or Metro that was messed up, now it should be resolved.

blackxored commented 7 years ago

I'm having the same issue:

yarn list v0.24.6
├─ jest@20.0.4
├─ react-native@0.45.1
└─ react@16.0.0-alpha.12
✨  Done in 1.00s.

Did clean node_modules and yarn.lock.

brobiden commented 7 years ago

Still having this issue as well

meinto commented 7 years ago

Unfortunately i still have this issue. I removed yarn.lock, removed the folder node_modules and installed the modules again.

npm run test still throws the same error:

 Test suite failed to run

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

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
      at Object.<anonymous> (node_modules/react-native/jest/setup.js:30:1)

The test command in my package.json looks like follows:

{
  "scripts": {
     "test": "cross-env NODE_ENV=test jest --silent --collectCoverageFrom='[\"src/**/*.{js}\"]'"
  }
}

@cpojer - you said maybe a dependency for jest was messed up. Do i have to install/update a package? The latest version of jest-cli is still 20.0.4.

khanghoang commented 7 years ago

@tobiasMeinhardt can you create a fresh RN project and try to run jest on it. I know it's weird but let try it, if it works then maybe you need to clean your project. In my case, I need to clean everything to make it work again.

meinto commented 7 years ago

Thanks for all the help!

My project is a library with an Examples folder. This was the problem. In this example folder i have multiple example projects with - of course - a node_modules folder in each of it.

To solve my problem i added modulePathIgnorePatterns to my jest configuration of my root library project.

package.json of my library project:

{
  "jest": {
    "modulePathIgnorePatterns": ["<rootDir>/Examples/"],
  }
}

After that i ran npm test -- --no-cache. This solved it for me.

zheng-liu-seattle commented 7 years ago

i am also having this issue, on mac, tried to clean node_modules, but still see it.

phillbaker commented 7 years ago

Was seeing a similar issue on our CI runs:

Test suite failed to run

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

      at Resolver.resolveModule (../../../../../<root>/node_modules/jest-resolve/build/index.js:169:17)
      at Object.<anonymous> (node_modules/react-native/jest/setup.js:23:1)

root issue for us was that ./node_modules was symlinked to another node_modules elsewhere.

ancyrweb commented 7 years ago

Running npm test -- --no-cache made it for me

peterleilei86 commented 7 years ago

Having the same issue! OS: Mac OS, jest config:

"jest": {
    "preset": "react-native",
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
      ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js",
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/fileTransformer.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "moduleNameMapper": {
      "\\.(css|less)$": "identity-obj-proxy",
      "^React$": "<rootDir>/node_modules/react"
    },
    "modulePathIgnorePatterns": [
      "<rootDir>/node_modules/react-native/Libraries/"
    ],
    "verbose": true
  }

package.js:

"dependencies": {
    ......
   "react": "16.0.0-alpha.13",
   "react-dom": "16.0.0-alpha.13",
   "react-native": "0.45.1"
},
"devDependencies": {
    "@types/jest": "^20.0.2",
    "babel-cli": "^6.2.0",
    "babel-core": "^6.10.4",
    "babel-jest": "^20.0.3",
    "babel-preset-react-native": "^1.9.2",
    "jest": "^20.0.4",
    "jest-cli": "^20.0.4",
    "react-test-renderer": "^15.6.1",
    "regenerator-runtime": "^0.10.5",
    "ts-jest": "^20.0.6",
     #......
}
peterleilei86 commented 7 years ago

@phillbaker Hey I am having the same issue. Can you elaborate the root issue on your case and the solution? Thanks so much

phillbaker commented 7 years ago

@peterdev6 we were seeing this issue only on our CI, where we "cache" our node_modules and then before running npm test we would ln the ./node_modules to that cached directory. My guess is that there's a hardcoded path to load setupDevtools, or something similar, which doesn't like the symbolic link. In our case, switching to just mving or cping the cached directory to a local reference worked.

Hope it helps.

peterorum commented 7 years ago

I had a newer version of react-native-svg that expected a later version of react native than i'm using, so i re-installed the older version of react-native-svg for the time-being.

sarovin commented 7 years ago

@tobiasMeinhardt You have your test file in __tests__ dir? I have the same issue but if i move the test file in to __tests__ directory, it works.

SupriyaKalghatgi commented 7 years ago

This issue keeps appearing every now and then I am frustrated with it

sarovin commented 7 years ago

@SupriyaKalghatgi i have this issue if i add extra options in package.json... i have fixed with this:

"jest": {
    "preset": "jest-react-native"
  }
SupriyaKalghatgi commented 7 years ago

@sarovin Thats an expo app of mine So i have included

"jest": {
    "preset": "jest-expo",
}
ptmt commented 7 years ago

In our case, it was related to a custom watchman config. You can check if it's the case for you with jest --no-watchman

dlindahl commented 7 years ago

I've tried all of the above suggestions and I still can't get rid of this error.

// test - src/utils/__tests__/foo.test.js
describe('Foo', () => {
  it('exists', () => {
    expect(true).toBeTruthy()
  })
})
// package.json - Jest config
  "jest": {
    "preset": "react-native",
    "roots": ["src"]
  }

If I remove one of those config options, the test suite runs fine. But if they are both present, I get:

 ● Test suite failed to run

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

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
      at Object.<anonymous> (node_modules/react-native/jest/setup.js:30:1)
larsonjj commented 7 years ago

I ran into the same problem today. Was able to solve it with the following jest config within package.json:

"jest": {
    "preset": "jest-expo",
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "moduleFileExtensions": [
      "js",
      "jsx",
      "json"
    ],
    "moduleDirectories": [
      "packages",
      "node_modules"
    ]
  },
trevorah commented 7 years ago

I've had the same issue on macOS. Tried clearing all npm/yarn caches & node_modules, but I would still get the same error. Tried a npm test -- --no-cache, but still no dice (possibly because of a lerna setup where --no-cache wasnt reaching jest for some of the packages).

The only thing that would fix it was a rm -rf $TMPDIR/jest_dx.

chenasraf commented 6 years ago

Also have the same issue:

Cannot find module 'enzyme' from 'enzyme_setup.js'
   at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)
   at Object.<anonymous> (test/enzyme_setup.js:1:108)

Here's my enzyme_setup.js:

const configure = require('enzyme').configure
const Adapter = require('enzyme-adapter-react-16')

configure({ adapter: new Adapter() })

And here's my package.json's jest section:

"jest": {
    "resolver": "jest-webpack-resolver",
    "transform": {
      "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json"
    ],
    "moduleDirectories": [
      "node_modules",
      "app"
    ],
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/test/unit/__mocks__/fileMock.js",
      "\\.(css)$": "identity-obj-proxy"
    },
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],
    "setupFiles": [
      "<rootDir>/test/enzyme_setup"
    ],
    "verbose": true
  },
  "jestWebpackResolver": {
    "webpackConfig": "<rootDir>/webpack/webpack.common.js"
  }

I also tried with different presets and without presets, none showed any change.

LRNZ09 commented 6 years ago

Strangely I've resolved by doing:

yarn test will initially fail with Cannot find module 'setupDevtools' from 'setup.js' error. yarn test --no-cache will fail with the same error. yarn test --no-watchman will fail again. yarn test will pass.

Don't know why but it works now. Related issue here: https://github.com/expo/jest-expo/issues/14

pintocarlos commented 6 years ago

Try running jest with the flag --no-cache once and then rerunning without the flag. That fixed it for me.

In my case, I started experiencing the setup.js error for no particular reason, given that there were no changes on source control and it was working fine on other local environments and CI.

dwilhel1 commented 6 years ago

I had this issue as well on my OSX / Mac platform and found a solution this morning. As it turns out, Babel has documentation which specifically explains how to setup Jest when used together. In summary, here's what I did:

Install NPM Modules

npm install --save-dev babel-jest
npm install --save-dev babel-preset-env

or with one command:

npm install i -d babel-jest babel-preset-env

Create .babelrc Config File

{
  "presets": ["env"]
}

Modify package.json

{
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "transform": {
      "^.+\\.jsx?$": "babel-jest"
    }
  }
}

Here is some further reading on why the env preset configuration is needed. Happy coding!

Quadriphobs1 commented 6 years ago

Appears this is still an issue I am facing right now, apparently was happening a while back but a few moments later started with this error... checked several issue/overflow questions tried many suggestion but no attempt succeeded.

marcelkalveram commented 6 years ago

I've had this issue after an update from React Native from 0.55 to 0.56.

We're using https://github.com/infinitered/jest-preset-ignite and this PR solved the issue for us: https://github.com/infinitered/jest-preset-ignite/pull/3/files.

The diff should also work for non-ignite projects, or at least help you figure out what's needed for the 0.56 migration.

DZuz14 commented 5 years ago

For anyone going absolutely insane...might be worthwhile to just give this a shot, this is all it took for me.

  "jest": {
    "preset": "./node_modules/babel-jest",
    "roots": [
      "<rootDir>/__tests__"
    ]
  }
raven-chen commented 5 years ago

By digging into the react-native/jest/setup.js. I found if I change

jest.mock('setupDevtools') to jest.mock('../Libraries/Core/Devtools/setupDevtools.js').

This error will be gone.

But another one Cannot find module 'InitializeCore' showed up. Changing this to jest.mock('../Libraries/Core/InitializeCore.js') resolved the error too. but another one showed up.

So it looks like something wrong with loading the modules inside ../Libararies. keep invesgitating..

ltfschoen commented 5 years ago

I just encountered this error on macOS. I just had to update from react-native 0.55.3 to 0.56 in this commit https://github.com/paritytech/parity-signer/pull/209/commits/961dbba30feb785470551de6cc9a8770b72bacc6

adrian-moisa commented 5 years ago

Several bugfixes were needed, and I managed to get the tests running:

willgriffiths commented 5 years ago

I had accidentally added testEnvironment="node" instead of just using the "react-native" preset and removing it fixed it for me.

module.exports = {
  testEnvironment: "node", // <---- removing this 
  preset: "react-native"
};
thymikee commented 5 years ago

RN preset uses node environment so what you did is invalidating Jest cache. You can do so with a command jest --clearCache

youngjuning commented 3 years ago

My situation:

react-native: 0.59.10 react: 16.8.3 jest: 24.9.0 babel-jest: 24.9.0

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.