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.

Jest Tests Handling New UUID Version

See original GitHub issue

The uuid upgrade to 8.3.2 is causing my Jest tests to fail due to the new export format for that package. I’ve tried many different combinations of using transformIgnorePatterns to try and get Jest to transform that package however nothing seems to take. I’ve got a couple packages that use uuid so the version used by react-tooltip is scoped to that package, i.e.: it’s installed in node_modules/react-tooltip/node_modules/uuid.

// In a jest config
"transformIgnorePatterns": [
  "<rootDir>/node_modules/(?!(uuid|react-tooltip/node_modules/uuid))"
]

I’ve even tried using moduleNameMapper to get Jest to use a different uuid package but that does not take either.

// In a jest config
"moduleNameMapper": {
  "^.+\/react-tooltip\/node_modules\/uuid.+$": "<rootDir>/node_modules/uuid"
}

The only thing I’ve gotten to work to fix it is to use Jest to mock the tooltip module entirely.

// In a jest test
jest.mock('react-tooltip', () => ({
  ReactTooltip: () => <div>Hello I am a tooltip</div>,
}));

Perhaps there a trick to getting Jest to transform, ignore transforms, or map modules for nested package installs like this but I haven’t been able to find it.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
trevrdspcdevcommented, Oct 15, 2022

@aedryan I was having the exact same issue but was able to resolve it in my own project using the fix discussed here. Part of the issue appears to be that the transformIgnorePatterns option does not work when you have a .babelrc in your project root. It requires using babel.config.js instead. From what I have read, this information is not well documented. It is also not included in the error message we have been seeing, which is unfortunate.

Following the instructions at the link I posted above, I:

  • renamed my .babelrc file to babel.config.js and prepended module.exports = to the babel config object in that file (while also changing from double to single quotes in my case)

  • added the following to the jest config in my project’s package.json:

    "transformIgnorePatterns": [
      "/node_modules/(?!uuid).+\\.js$"
    ]
    

This got my Jest unit tests passing successfully. I hope this info helps in your case too!

2reactions
danielbarioncommented, Oct 18, 2022

@trevrdspcdev @aedryan can you guys try again with version 4.4.2, please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to mock uuid with Jest - Stack Overflow
Mock it by using mockImplementation . import uuid from 'uuid/v4'; jest.mock('uuid/v4'); describe('mock uuid', () => { it('should return ...
Read more >
How To Mock uuid In Jest - I Like Kill Nerds
When it comes to mocking dependencies in Jest, it couldn't be easier. You can create an actual mock module that gets loaded in...
Read more >
Snapshot test for component with uuid - facebook/jest - GitHub
I have an interesting test issue. I've got custom components that use the uuid node package to generate unique IDs which I can...
Read more >
Expect - Jest
The expect function is used every time you want to test a value. You will rarely call expect by itself. Instead, you will...
Read more >
A missing Jest mock feature - Medium
Each time uuid function is called in our test code, it will have a new, different value. This allowed me to recognize different...
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