Compatibility issue with typescript, rewire & jest
See original GitHub issueTell us about your environment
- ESLint Version: 4.19.1
- Node Version: 10.2.1
- npm Version: 6.4.1
What parser (default, Babel-ESLint, etc.) are you using?: tslint ¯_(ツ)_/¯
Please show your full configuration: None, really
Using typescript with rewire(https://github.com/jhnns/rewire) and jest(https://github.com/facebook/jest) for testing ended up with the following error:
Cannot find module '../conf/replacements' from 'rules.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
at Object.<anonymous> (node_modules/rewire/node_modules/eslint/lib/rules.js:14:26)
This occurs because rewire requires eslint as a dependency and jest tries to do something with all imported modules, but seemingly can’t handle .json files
It can be 'solved ’ by adding the .json extension in rules.js ruleReplacements import
const ruleReplacements = require("../conf/replacements.json").rules;
and the following in a typescript project’s package.json:
"jest": {
"transform": {
"\\.json$": "<rootDir>/your/dummy/emptyFile.js"
},
I hope this might help some people in the future
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Using rewire with Jest in TypeScript - The (not so) Weekly Fitz
Testing an inacessible / unexposed method via rewire. So, for this you'll need jest, ts-jest, @types/jest (so your IDE doesn't complain), @types/rewire (same), ......
Read more >Test or mock a not-exported function with Jest using TypeScript
I've seen SO answers recommending some libraries like rewire but it seems they are not really TypeScript compatible yet. The other way would...
Read more >rewire - npm
Please note: The current version of rewire is only compatible with CommonJS modules. See Limitations. Installation. npm install rewire ...
Read more >react-app-rewire-typescript - npm package - Snyk
An important project maintenance signal to consider for react-app-rewire-typescript is that it hasn't seen any new versions released to npm in the past...
Read more >Are most people not using ESM (import/export) syntax even in ...
I will try tomorrow to configure Babel for Jest with v14 of Node. I'm not sure if the transpilation is the cause of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@platinumazure You’re welcome ! And yes, you’re definitely right. In order to run tests in Typescript, I used ts-jest and had to customise jest’s
"moduleFileExtensions"
parameter in my package.json, to add typescript files extensions, like so:when I should have updated it like this:
You see, by default it has .json in there, but setting it overwrites the defaults completely. And without .json in there, jest will fail when a file requires a .json file.
No, you are not. Jest apparently doesn’t bother checking wether it is a JS module or not. And I have no idea how much work it would take on their side to do that. On the other hand, simply adding the .json extension, while not a real fix for Jest’s mishandling, helps fixing it.
I’ve just opened an issue on jest’s. Because it shouldn’t crash like this when some dependeny’s random dependency requires a .json (see https://github.com/facebook/jest/issues/7158)