Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Jest fail to mock a dependency and throw an error
$ jest tests/units/WebsiteBuilding/Infra/HttpTranslationMessageRepositoryTest.js
FAIL tests/units/WebsiteBuilding/Infra/HttpTranslationMessageRepositoryTest.js
โ Test suite failed to run
Cannot find module './package' from 'index.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:160:17)
at Object.<anonymous> (node_modules/got/index.js:19:13)
Adding the .json suffix manually in the node_modules/got/index.js file resolve the issue.
What is the expected behavior?
Jest should detect that the file referenced by ./package is a JSON file and work correctly with it. Omitting the extension when requiring a json file is a valid usage of the require function:
LOAD_AS_FILE(X)
1. If X is a file, load X as JavaScript text. STOP
2. If X.js is a file, load X.js as JavaScript text. STOP
3. If X.json is a file, parse X.json to a JavaScript Object. STOP
4. If X.node is a file, load X.node as binary addon. STOP
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Reproduced on jest v19.0.0 and v19.0.1.
Node v7.6.0
Can you provide a repro?
I'm trying to reproduce the behavior in a simple repository but I do not succeed. Maybe it's link to a more complex environment parameter that I do not succeed to reproduce.
I just notice that event if do not mock the module the error is throw
Ok, I think this is not a Jest issue. Feel free to ask more on the Jest discord channel and respond here if you can create a repro so if there is a bug in Jest we can fix it.
I find the source of the issue!
In my jest config I have set these extensions: "moduleFileExtensions": [ "js", "jsx" ],.
If I add the json extension here "moduleFileExtensions": [ "js", "jsx", "json" ], the error is fixed!
It seems that's a pretty dangerous side effect! I think that the js and json extension should be always activated as there are part of the default behavior. The dependencies of a project will relies on this behavior.
The source of this behavior are in the jest-resolve package: https://github.com/facebook/jest/blob/master/packages/jest-resolve/src/index.js#L91
Most helpful comment
I find the source of the issue!
In my jest config I have set these extensions:
"moduleFileExtensions": [ "js", "jsx" ],.If I add the
jsonextension here"moduleFileExtensions": [ "js", "jsx", "json" ],the error is fixed!It seems that's a pretty dangerous side effect! I think that the
jsandjsonextension should be always activated as there are part of the default behavior. The dependencies of a project will relies on this behavior.The source of this behavior are in the jest-resolve package: https://github.com/facebook/jest/blob/master/packages/jest-resolve/src/index.js#L91