-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Description
🐛 Bug Report
When running require within a block of code that contains a template string (e.g. require(images/${imageURL})), babel-jest fails to resolve the template string properly when running jest --watch. It works fine without the --watch flag.
To Reproduce
Run jest --watch with babel-jest set as the transformer on a file that contains the following:
const ImageLink = ({ linkPath, linkLabel, imageSource }: ImageLinkProps) => (
<Link to={linkPath}>
<img alt={`Navigation link to ${linkLabel}`} src={require(`$images/${imageSource}`)} />
<span>{linkLabel}</span>
</Link>
);Expected behavior
The above code is valid and works without issue when transformed by babel-jest without the --watch flag. I expect it to work without issue with --watch set as well. However, I currently get the following error from jest (which in turn doesn't run, due to a non-existent file):
Could not locate module $images/${imageSource} mapped as:
/home/thisissami/code/s2sFrontEnd/static/images/${imageSource}.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^\$images(.*)/": "/home/thisissami/code/s2sFrontEnd/static/images$1"
},
"resolver": null
}This clearly means that babel-jest is not resolving the template string within the require, and is rather treating the template string as a normal string. When changing the above code to the following, babel-jest transforms without issue with the --watch mode:
const ImageLink = ({ linkPath, linkLabel, imageSource }: ImageLinkProps) => {
const src = `$images/${imageSource}`;
return (
<Link to={linkPath}>
<img alt={`Navigation link to ${linkLabel}`} src={require(src)} />
<span>{linkLabel}</span>
</Link>
);
};Run npx envinfo --preset jest
Paste the results here:
System:
OS: Linux 4.15 Pop!_OS 18.04 LTS
CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Binaries:
Node: 10.8.0 - ~/.nvm/versions/node/v10.8.0/bin/node
npm: 6.4.0 - ~/.nvm/versions/node/v10.8.0/bin/npm
npmPackages:
jest: ^23.5.0 => 23.6.0