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

default moduleFileExtensions order is problematic for typescript projects #9834

Closed
adamborowski opened this issue Apr 17, 2020 · 4 comments · Fixed by #10572
Closed

default moduleFileExtensions order is problematic for typescript projects #9834

adamborowski opened this issue Apr 17, 2020 · 4 comments · Fixed by #10572

Comments

@adamborowski
Copy link

adamborowski commented Apr 17, 2020

Hi. I think that the default order of extensions in moduleFileExtensions can be very problematic if you have typescript files in a project.
https://jestjs.io/docs/en/configuration#modulefileextensions-arraystring

As doc says, the lookup of module will have the order as set in this array.

This default order defines thet json files have precedence over ts files.

Why this can be problematic? Because sometimes you have both json and ts/js files of the same name.
For example if you use react-intl, you have messages.json next to messages.ts

This order breaks tests as file may import { messages } from '[...]/i18n/messages';

As the result, the json file is loaded, not messages.ts. This causes improper module to be loaded during tests, causing all tests that use translations to fail.

I recommend to reorder extensions in default setup.

moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json']

I think this will bring less problems than just mentioning

We recommend placing the extensions most commonly used in your project on the left, so if you are using TypeScript, you may want to consider moving "ts" and/or "tsx" to the beginning of the array.

@adamborowski adamborowski changed the title moduleFileExtensions order is problematic for typescript projects default moduleFileExtensions order is problematic for typescript projects Apr 17, 2020
@jeysal
Copy link
Contributor

jeysal commented May 25, 2020

The order of moduleFileExtensions can have performance implications, if we have lots of misses when checking for the first few possible extensions. Hence our recommendation in the docs.
For this reason it's probably at the very least too early to move TS in front of JS. But I would be down with moving JSON behind TS, as there are usually few JSON modules. If you want to send a PR changing that, I think we can accept it, although only for the next major version since it could be breaking.

@jackneer
Copy link
Contributor

jackneer commented Oct 2, 2020

May I help this? thanks!

@SimenB
Copy link
Member

SimenB commented Oct 2, 2020

Of course! 👍

@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 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants