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

moduleNameMapper only works with regexes matching the whole path #1624

Closed
fson opened this issue Sep 6, 2016 · 4 comments
Closed

moduleNameMapper only works with regexes matching the whole path #1624

fson opened this issue Sep 6, 2016 · 4 comments

Comments

@fson
Copy link
Contributor

fson commented Sep 6, 2016

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

The moduleNameMapper configuration only works with regexes that match the whole path. I found this out when working on facebook/create-react-app#584. When using this configuration the modules were stubbed correctly:

    moduleNameMapper: {
      '^.+\\.(jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'),
      '^.+\\.css$': resolve('config/jest/CSSStub.js')
    },

But changing it to following caused them to not be stubbed:

    moduleNameMapper: {
      '\\.(jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'),
      '\\.css$': resolve('config/jest/CSSStub.js')
    },

Upon further investigation, I found out that this line in jest-resolve uses the same regex to replace the module name with the mapped name, and if the regex doesn't match the whole path, it doesn't get replaced correctly.

What is the expected behavior?

Expected to be able to use regexes like \\.css$ that only match the end of the path.

@cpojer
Copy link
Member

cpojer commented Sep 14, 2016

Yes that is indeed a bug! Is this something you (or anyone else who is running into this) could help out with? The code should be somewhere around here: https://github.com/facebook/jest/blob/2699ed50ae4f849dfc384d3a4f4c4687b9fb24b0/packages/jest-resolve/src/index.js#L201

@zackargyle
Copy link
Contributor

I can take a stab. 👍 Seems like it might be a really easy fix.

@zackargyle
Copy link
Contributor

PR up for fix: #1723

@cpojer cpojer closed this as completed Sep 17, 2016
@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 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants