Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest crashes when testing using Typescript & Rewire #7158

Closed
El-Fitz opened this issue Oct 13, 2018 · 4 comments
Closed

Jest crashes when testing using Typescript & Rewire #7158

El-Fitz opened this issue Oct 13, 2018 · 4 comments

Comments

@El-Fitz
Copy link

El-Fitz commented Oct 13, 2018

🐛 Bug Report

Using typescript with rewire(https://github.com/jhnns/rewire) and 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 eslint's 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

To Reproduce

Use rewire with Jest in Typescript

Expected behavior

I expect the tests to run and Jest not to blow up when someone requires a .json file in some dependency's random dependency without specifying the extension. Apparently some it is supposed to work...

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: macOS 10.14
    CPU: x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  Binaries:
    Node: 10.2.1 - /usr/local/bin/node
    npm: 6.4.1 - /usr/local/bin/npm
  npmPackages:
    @types/jest: ^23.3.4 => 23.3.4
    jest: ^23.6.0 => 23.6.0
@SimenB
Copy link
Member

SimenB commented Oct 14, 2018

You didn't provide a reproduction like the template asked (you need config to run with typescript, so you've changed something). However; since jest, like node, supports requiring json files, mye guess is that you've changed moduleFileExtensions , see https://jestjs.io/docs/en/configuration#modulefileextensions-array-string. Keep json in it

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@El-Fitz
Copy link
Author

El-Fitz commented Oct 15, 2018

So... you were right. I did not add "json" when customising moduleFileExtensions, my bad.
To be honest, I genuinely thought it was a bug. But thanks !

@SimenB
Copy link
Member

SimenB commented Oct 15, 2018

No worries 🙂

See #7160, we'll improve the error message in this case in the future

SimenB added a commit that referenced this issue Oct 15, 2018
<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. The two fields below are mandatory. -->

<!-- Please remember to update CHANGELOG.md in the root of the project if you have not done so. -->

## Summary

Inspired by #7158, I finally decided to throw a better error in the cases where people have custom `moduleFileExtension` resulting in more or less obscure errors.

I've tackled 2 different issues here (in separate commits, happy to split them up into separate PRs if you want).

1. If you require a file without the file extension, we try to look for files matching _with_ a file extension and list them out. Also tell the user to either include file extension in the `require` or update `moduleFileExtension`.
2. If `js` is missing from `moduleFileExtension`, jest is unable to inject into the runtime. I decided to throw an explicit configuration error rather than fixing `jest-jasmine`'s `require`, as we'd also need all of our dependencies to do the same (e.g. `source-map` throws if we do `moduleFileExtension: []` now).

Fixes #4025.

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

## Test plan
Integration tests added

<!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes UI. -->
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants