question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compatibility issue with typescript, rewire & jest

See original GitHub issue

Tell 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
El-Fitzcommented, Oct 16, 2018

@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:

"moduleFileExtensions": [
			"ts",
			"tsx",
			"js"
		]

when I should have updated it like this:

"moduleFileExtensions": [
			"ts",
			"tsx",
			"js",
			"json"
		]

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.

2reactions
El-Fitzcommented, Oct 13, 2018

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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found