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.

Add the required "default" export mocking caveat to Jest migration guide.

See original GitHub issue

Clear and concise description of the problem

It took me a while to realise that I needed to explicitly mock default module exports in vitest. Since this is a rather big change from the way we mock modules in jest, I think it’s worth to mention this in the Jest migration guide.

This is well documented in https://vitest.dev/api/#vi-mock, but is easy to miss as it’s not a part of the general mocking guide.

Suggested solution

Add a quick note about default export mocking in https://vitest.dev/guide/migration.html#migrating-from-jest. I think this will help people alot.

Alternative

Provide better error messages when people accidentally mock named exports with vi.mock. For example cannot read property 'get' of undefined, did you intend to mock the "default" export? See https://vitest.dev/api/#vi-mock for details.

Additional context

No response

Validations

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jereklascommented, Aug 9, 2022

Or maybe just !target.hasOwnProperty(prop). I wont be able to get to this tonight, but can try putting up a PR fix along with some documentation updates tomorrow. I’ll also add a test case for the explicit case where someone sets the export to undefined.

1reaction
jereklascommented, Aug 9, 2022

This should be able to be closed as part of this PR? I guess I didn’t update any documentation with that fix, do you want an update in either of the documents linked in this issue @sheremet-va ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating to Jest
If you'd like to try out Jest with an existing codebase, there are a number of ways to convert to Jest:
Read more >
0.61.0 - Jest "Cannot find module ..." #26579
js file in tests/__mocks__ and added exports for the modules I need to mock: import * as ReactNative from "react-native"; export ...
Read more >
Jest mocking default exports - require vs import - Stack Overflow
When mocking the default export of a dependency of a module that is being tested, the tests suite fails to run if the...
Read more >
Migration Guide - Vitest
Jest has their globals API enabled by default. ... When mocking a module in Jest, the factory argument's return value is the default...
Read more >
Mocking | Detox - Open Source
This guide assumes you are testing a React Native app with Detox. Please note that you cannot apply mocking techniques familiar from the...
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