jest: Importing json files result in SyntaxError: Unexpected token m in JSON at position 0
đź’Ą Regression Report
After upgrade of jest-runtime to 24.8.0 the json file import stopped working because the json file is not imported but transformed into following string
module.exports = "permanentComponentConfigurations.json";
As a workaround I provided a json transformation like so
package.json
"transform": {
".+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
".+\\.css$": "<rootDir>/config/jest/cssTransform.js",
".+\\.json": "<rootDir>/config/jest/jsonTransform.js",
"(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
(The configuration from package.json is based on Create-React-App)
jsonTransform.js
module.exports = {
process(src) {
return src;
}
};
Last working version
Worked up to version: 24.7.1
Stopped working in version: 24.8.0
To Reproduce
Import a json file in js like
import PERMANENT_CONFIGURATIONS from 'core/service/config/permanentComponentConfigurations.json';
With 24.8.0 json files are transformed as well. But the resulting json file import results to following content
module.exports = "permanentComponentConfigurations.json";
Expected behavior
The imported json file should be JSON.parsed like before with 24.7.1 per default. If someone needs custom transformation, providing a transformation file is fine, but it should not be a requirement.
Link to repl or repo (highly encouraged)
Please provide either a repl.it demo or a minimal repository on GitHub.
Issues without a reproduction link are likely to stall.
Run npx envinfo --preset jest
System:
OS: Windows 10
CPU: (4) x64 Intel(R) Xeon(R) CPU E3-1226 v3 @ 3.30GHz
Binaries:
Node: 10.15.3 - C:\Program Files\nodejs\node.EXE
Yarn: 1.15.2 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 15 (5 by maintainers)
just a small addition for people accidentally stepping over this:
if you’ve been using
28.8 was a breaking change as it will break your json imports. You might want to e.g. restrict the transform to only transform js files.
@sakulstra After 3 hours of debugging, I must say… You saved my day! I’m almost in tears !!!
The default json transform should work fine, you just need to make sure your own transform does not hit it.
@scotthovestadt might consider reverting and landing in 25? We can add a warning if the pattern matches
.json
today saying that json is not supported until 25, and if the user does not want to transform json to fix the pattern?I am getting this issue still, I pushed what I have to a branch: https://github.com/amzn/style-dictionary/tree/updating-jest
Using Jest 24.8.0, my transform looks like:
Travis build with tests failing: https://travis-ci.org/amzn/style-dictionary/builds/552967613
Hi Guys! Im using jest 24.8.0 and this is my transform config
“transform”: { “^.+\.json”: “<rootDir>/scripts/test/transformJSON.js”, -> I had to add this dummy transformer for JSONs to work, what am I doing wrong? “^.+\.js”: “babel-jest”, “^.+\.(ts|js|html)$”: “ts-jest” }
I’d guess this is from https://github.com/facebook/jest/pull/8278, not sure though. Could you provide a reproduction repo?