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

Throw explicit errors for common moduleFileExtension failures #7160

Merged

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Oct 14, 2018

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.

Test plan

Integration tests added

@thymikee
Copy link
Collaborator

Ping me tomorrow once I have access to the computer, but the idea sounds great!

@SimenB
Copy link
Member Author

SimenB commented Oct 14, 2018

image

@SimenB SimenB force-pushed the detect-files-outside-moduleFileExtensions branch from 6bf6bc3 to 2e36bb5 Compare October 14, 2018 12:57
@codecov-io
Copy link

codecov-io commented Oct 14, 2018

Codecov Report

Merging #7160 into master will increase coverage by 0.04%.
The diff coverage is 70%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7160      +/-   ##
==========================================
+ Coverage   66.43%   66.48%   +0.04%     
==========================================
  Files         253      253              
  Lines       10658    10684      +26     
  Branches        3        4       +1     
==========================================
+ Hits         7081     7103      +22     
- Misses       3576     3580       +4     
  Partials        1        1
Impacted Files Coverage Δ
packages/jest-config/src/normalize.js 91.22% <100%> (-0.22%) ⬇️
packages/jest-runtime/src/index.js 69.02% <57.14%> (+1.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5cd7d17...6440839. Read the comment docs.

try {
const slashedDirname = slash(dirname);

const matches = glob(`${pathToModule}.*`)
Copy link
Member Author

@SimenB SimenB Oct 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to have the hastemap here instead of hitting the FS again, but unless I'm missing something it's not available from runtime

@SimenB
Copy link
Member Author

SimenB commented Oct 15, 2018

@thymikee sup?

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

case 'moduleFileExtensions': {
value = options[key];

// If it's the wrong type, it can throw at a later time
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can mention about not being able to inject Jest into runtime here?

From the PR summary:

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).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the comment, or in the error message to the user?

Copy link
Member Author

@SimenB SimenB Oct 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment in particular is about the Array.isArray check, if that wasn't obvious. jest-validate will complain about its type if it's not an array, normalize doesn't have to care

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment about type is fine. I was thinking about leaving a message why we care about "js" being always present

packages/jest-runtime/src/index.js Show resolved Hide resolved
@SimenB
Copy link
Member Author

SimenB commented Oct 15, 2018

@thymikee how about now?

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍
I thought we already added the integrity fields to lockfile, but maybe it wasn't merged yet.

@SimenB
Copy link
Member Author

SimenB commented Oct 15, 2018

I thought we already added the integrity fields to lockfile, but maybe it wasn't merged yet.

We had, but some PR from before we did so probably got merged

@SimenB SimenB merged commit 3157c8a into jestjs:master Oct 15, 2018
@SimenB SimenB deleted the detect-files-outside-moduleFileExtensions branch October 15, 2018 14:30
However, Jest was able to find:
'./some-json-file.json'

You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js'].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is such a good error

// works in Node normally.
throw createConfigError(
errorMessage +
"\n Please change your configuration to include 'js'.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove 'js' from the config and just always use it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean just pushing it in? If so, I thought about, but decided against it as I think it would be confusing for the user that we changed their configuration. Better that they explicitly list the config. Do you disagree with that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally agree with being explicit, but in this case not including it breaks the suite, so why have the user list it at all? It may be better to have a note that says "Jest will always check for '.js' extensions" in the docs and then no one has to think about it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, your response was lost in the ether.

I still think it's confusing if you get a js file required without saying the should be without an extension. Requiring the user to be explicit is less magical. Send a PR changing it, and I won't close it, though 😉

@rickhanlonii
Copy link
Member

Sorry I'm late to the party but this is really fly, good work 👍

@github-actions
Copy link

This pull request 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Cannot find module './createSpy' from 'jasmine-light.js'
5 participants