Mocking scoped package libraries
See original GitHub issueI’ve been working on migrating a project which we ejected some time ago back into CRA.
I’ve come across a problem with the way we are mocking scoped packages. We were using the moduleNameMapper
in the jest section of our package.json like this:
package.json
"jest": {
"moduleNameMapper": {
"^@org-name/package-one$": "<rootDir>/__mocks__/org-name/package-one/dist/package.js",
"^@org-name/package-two$": "<rootDir>/__mocks__/org-name/package-two/build/index.js"
}
}
When I add this to the un-ejected CRA project I get the following error:
These options in your package.json Jest configuration are not currently supported by Create React App:
• moduleNameMapper
If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.
Any ideas how I can get these mocks working?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Manual Mocks - Jest
Scoped modules (also known as scoped packages) can be mocked by creating a file in a directory structure that matches the name of...
Read more >Mocking with Jest: Taking Advantage of the Module System
Manul mocks for npm packages will be used automatically, even without calling jest. mock (this doesn't apply to built-in modules).
Read more >Jest Mocking node_modules With Nested File - Stack Overflow
It appears that jest does not support mocking scoped packages (more info here). So you have two options. 1. Define your mock inline...
Read more >Manual Mocks - Coding Ninjas CodeStudio
We can also mock the Scoped Modules or the Scoped Packages by creating a file in the directory structure that matches the name...
Read more >Mocking - Stencil.js
If you want to mock a scoped package like @capacitor/core , you'll have to create the file as __mocks__/@capacitor/core.ts .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have created PR https://github.com/facebook/create-react-app/pull/6633 to support merging of external
moduleNameMapper
configuration with react-scripts’ internal configuration. I believe its a win-win solution. Hopefully everyone agrees.I was trying to run the Jest test suite in my local environment! I created a jest.config.js in the root directory of my project and moved the configs of Jest from package.json to jest.config.js
module.exports = { moduleNameMapper: { "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "jest-transform-stub", "\\.(css|less|scss|sass)$": "identity-obj-proxy" } };
and while running the test suite on local machine, I have provided the config path to IDE as jest.config.js and test suite is running successfully.also while running tests through cmd
npm run test
it is running without errors !