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

How does jest-resolver treat ! in imports and requires? #4868

Closed
kobyl opened this issue Nov 9, 2017 · 13 comments
Closed

How does jest-resolver treat ! in imports and requires? #4868

kobyl opened this issue Nov 9, 2017 · 13 comments

Comments

@kobyl
Copy link

kobyl commented Nov 9, 2017

I'm using script-loader for jquery plugins, and do a couple imports like this(in different places):

import 'script-loader!jquery-validation'
require('script-loader!jquery.maskedinput/src/jquery.maskedinput.js');

This is working at runtime on a browser, but not under Jest. I've tried adding script-loader.js to the __mocks__ folder next to node_modules and confirm that my mock code gets run when I just do

jest.mock('script-loader')
require('script-loader')

in a test file.

I'm wondering if the ! in the imports are messing up the resolution? The exact error I'm getting is:

● Test suite failed to run

  Cannot find module 'script-loader!jquery.maskedinput/src/jquery.maskedinput.js' from 'index.tsx'

    at Resolver.resolveModule (C:/Users/kobyL/AppData/Roaming/npm/node_modules/jest/node_modules/jest-resolve/build/index.js:179:17)
    at Object.<anonymous> (src/components/MaskedInput/index.tsx:12:1)

Thanks!

Koby

@mahesh-beehively

This comment has been minimized.

@SimenB
Copy link
Member

SimenB commented Jan 26, 2018

That's not valid identifiers, so jest doesn't understand them. You should use loaders (or whatever it's called nowadays) in webpack config, and utilize moduleNameMapper. See https://facebook.github.io/jest/docs/en/webpack.html

@SimenB SimenB closed this as completed Jan 26, 2018
@sebadiaz

This comment has been minimized.

@sebadiaz
Copy link

We appreciate Some samples. do you have one ?

@SimenB
Copy link
Member

SimenB commented Jun 19, 2018

Did you read the docs linked above?
If it doesn't work, please set up a small repo showing the error.

@Billy-
Copy link
Contributor

Billy- commented Nov 19, 2018

@SimenB using inline loader overrides is a valid use-case of webpack.. I think it would be amiss for jest not to support at least some way of working with them?

I tried adding the module to the moduleNameMapper jest config as "^!!custom-loader!": "<rootDir>/__mocks__/customLoader.js" as I figured this would bypass trying to resolve the module, but jest is tripping up and erroring trying to resolve the file (I'm guessing this happens before honouring mocks?)

@SimenB
Copy link
Member

SimenB commented Nov 19, 2018

moduleNameMapper is for string => string, it does not support a module that does loading (if I understand your example correctly), it's basically aliasing. So you have to implement it as a regex replacement.

What's your use case for a loader in this case? Could it be implemented as a transform?

I still recommend sticking the loader in your webpack config to avoid tool-specific syntax in your code.

An alternative is to implement the loader using a babel plugin.

@Billy-
Copy link
Contributor

Billy- commented Nov 19, 2018

I was hoping that import '!!custom-loader!my-file' would get caught by "^!!custom-loader!": "<rootDir>/__mocks__/customLoader.js" and never try to resolve !!custom-loader!my-file and instead just resolve to the mock? Just like mocking any other module or file?

My use-case is as follows:

I am using sass and have configured the sass/css loaders in my webpack.config. I have one occasion where I would like to import my main sass variables file into js using sass-variables-loader, for use in the JS code across the app. Hence I have a single instance where I need to override the normal webpack configuration and use the sass-variable-loader:

import vars from '!!sass-variable-loader!my-sass-vars.scss'

@SimenB
Copy link
Member

SimenB commented Nov 19, 2018

@Billy- I see, I'd think that was supposed to work... Can you try putting a breakpoint here and see what happens when your code runs? https://github.com/facebook/jest/blob/819cae5f18b6dc99c16ee2fc4e2df4a93b782290/packages/jest-resolve/src/index.js#L362-L398

I have one occasion where I would like to import my main sass variables file into js using sass-variables-loader

I still think it might be better to add that to exclude in your webpack config, or use oneOf and check for vars in the filename: https://webpack.js.org/configuration/module/#rule-oneof

@Billy-
Copy link
Contributor

Billy- commented Nov 19, 2018

Hmm, looks like it is hitting the mock now.. Must have been a typo or something cached. Sorry to waste your time, and thanks for the help!

duleorlovic added a commit to trkin/trk_datatables_js that referenced this issue Oct 3, 2019
This notation
```
require('imports-loader?define=>false,this=>window!datatables.net')(window, $)
```
break jest test
webpack-contrib/imports-loader#54
and they suggest to use loaders in webpack config
jestjs/jest#4868
but that will complicate installation in rails since we need to
configure webpacker to disable amd only for datatables.net dependency
@rafaeleyng
Copy link

@Billy- could you provide the working example? Is not clear how you've managed to make it work.

@taurheim
Copy link

taurheim commented Feb 2, 2021

I solved this by adding the following to my jest config:

"moduleNameMapper": {
  "!file-loader!(.*)": "<rootDir>/$1"
}

hope this helps others!

@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 10, 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

7 participants