question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocking scoped package libraries

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
nareshbhatiacommented, Mar 13, 2019

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.

2reactions
skarode96commented, May 25, 2019

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 !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found