Skip to content

moduleNameMapper only works with regexes matching the whole path #1624

@fson

Description

@fson

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions