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 with preset and additional config - override order #3565

Closed
shlomiassaf opened this issue May 13, 2017 · 2 comments · Fixed by #3689
Closed

moduleNameMapper with preset and additional config - override order #3565

shlomiassaf opened this issue May 13, 2017 · 2 comments · Fixed by #3689

Comments

@shlomiassaf
Copy link

Do you want to request a feature or report a bug?
bug (or intended behavior)

What is the current behavior?
When using a preset and an additional configuration (on top of the preset) the moduleNameMapper property is merged from both preset and the additional configuration.
The preset moduleNameMapper is assigned before the user-defined moduleNameMapper.

What is the expected behavior?
User-defined moduleNameMapper should execute before preset defined moduleNameMapper.
The current behaviour traps preset moduleNameMapper values thus not allowing override.

The logic that implements the above can be found here

Switching lines 80 with 81 should solve it.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Node 7.4
Jest 20.0.1
yarn 19

@markwhitfeld
Copy link
Contributor

I am experiencing the exact same issue and found the exact same lines to be the problem.
In particular, my issue is with the jest-preset-angular which provides this in the preset:

  "moduleNameMapper": {
    "(.*)": "<rootDir>/src/$1"
  },

Yip, you aren't going to get past that with the current setup.

Unfortunately switching those 2 lines won't fix it because the preset will always override your configuration if you specify the same regex. What we really need is for our configuration to be tested first but to still be able to override the preset.

I believe the following code change to those lines will do the trick:

options.moduleNameMapper = Object.assign(
    {},
    options.moduleNameMapper,
    preset.moduleNameMapper,
    options.moduleNameMapper);

This will add the settings from options first, then the ones from the preset, but then override with options in case one was overwritten by a preset.

I will submit a pull request for this fix shortly.

markwhitfeld pushed a commit to markwhitfeld/jest that referenced this issue May 30, 2017
Adjusted the oder so that the options mappings are checked first and then the preset mappings are checked.
The preset mappings can be overridden by the options mappings.
markwhitfeld pushed a commit to markwhitfeld/jest that referenced this issue May 30, 2017
Adjusted the oder so that the options mappings are checked first and then the preset mappings are checked.
The preset mappings can be overridden by the options mappings.
markwhitfeld pushed a commit to markwhitfeld/jest that referenced this issue May 30, 2017
Adjusted the order so that the options mappings are checked first and then the preset mappings are checked.
The preset mappings can be overridden by the options mappings.
markwhitfeld pushed a commit to markwhitfeld/jest that referenced this issue Jun 27, 2017
Adjusted the order so that the options mappings are checked first and then the preset mappings are checked.
The preset mappings can be overridden by the options mappings.
cpojer pushed a commit that referenced this issue Jun 27, 2017
* Fix options.moduleNameMapper override order with preset (#3565)

Adjusted the order so that the options mappings are checked first and then the preset mappings are checked.
The preset mappings can be overridden by the options mappings.

* Fix prettier linting issues

* Fix eslint issues

* Workaround in test for eslint sort-keys rule
tushardhole pushed a commit to tushardhole/jest that referenced this issue Aug 21, 2017
…jestjs#3689)

* Fix options.moduleNameMapper override order with preset (jestjs#3565)

Adjusted the order so that the options mappings are checked first and then the preset mappings are checked.
The preset mappings can be overridden by the options mappings.

* Fix prettier linting issues

* Fix eslint issues

* Workaround in test for eslint sort-keys rule
@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 13, 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

Successfully merging a pull request may close this issue.

2 participants