-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Fix mocking assets with special characters in the file path #584
Fix mocking assets with special characters in the file path #584
Conversation
The regexes in the Jest `moduleNameMapper` configs were a bit too strict, causing them to not pick up files with special characters like `@` in the file path. Change them to match anything with the correct file extension.
@@ -21,8 +21,8 @@ module.exports = (resolve, rootDir) => { | |||
const config = { | |||
moduleFileExtensions: ['jsx', 'js', 'json'], | |||
moduleNameMapper: { | |||
'^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'), | |||
'^[./a-zA-Z0-9$_-]+\\.css$': resolve('config/jest/CSSStub.js') | |||
'^.+\\.(jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are going to ignore everything before the end, you can just omit ^.+
altogether and just write \\.css$
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea. However, turns out the files don't get stubbed, if I change this to \\.css$
. This looks like a possible bug in Jest to me (unless it's only supposed to work with regexes that match the whole path). When given a non-anchored regex, this line doesn't replace the module name correctly.
I suggest that we merge this PR as-is, with the anchored regex, and we can open an issue in Jest about regexes like \\.css$
not working correctly.
Accepted. Feel free to merge it once you apply my comments :) |
@vjeux Mind if I merge this without your suggestion? Jest doesn't work with the partial regex at the moment. |
@fson 👍 |
Yeah go for it :) |
…#584) The regexes in the Jest `moduleNameMapper` configs were a bit too strict, causing them to not pick up files with special characters like `@` in the file path. Change them to match anything with the correct file extension.
The regexes in the Jest
moduleNameMapper
configs were a bit too strict,causing them to not pick up files with special characters like
@
in thefile path. Change them to match anything with the correct file extension.
Fixes #579.
Test plan:
template/src/[email protected]
.import './[email protected]';
toApp.js
.npm test
. Tests pass.