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.

Adding Jest Mocks

See original GitHub issue

Description

Through-out the various apps I work on, we always end up writing jest mocks for this lib.

Is there any opposition to adding file like <root>/jest/react-native-device-info-mock.js that can be imported into an app’s jest setup ?

There are a few other libs that follow a similar approach:

Use Case

As mentioned in various other issues related to mocks, this would greatly improve Developer Experience .


If we’re cool with it, I can make a PR adding it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
sem4phorcommented, Jan 7, 2022

hello,

i tried to use the mocks (by registrating it in the jest setup file) to change the return value of the hasNotch function but I can not get it working especially when doing two tests with different return values.

Is there a way you could provide an example usage for the mocks where you change the return value of a device info?

My Code: in setup setup.js

import mockRNDeviceInfo from 'react-native-device-info/jest/react-native-device-info-mock'
jest.mock('react-native-device-info', () => mockRNDeviceInfo)
0reactions
euclidesdrycommented, Nov 18, 2022

import mockRNDeviceInfo from ‘react-native-device-info/jest/react-native-device-info-mock’ jest.mock(‘react-native-device-info’, () => mockRNDeviceInfo)

Thank you, this worked well for me! @sem4phor

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manual Mocks - Jest
When a manual mock exists for a given module, Jest's module system will use that module when explicitly calling jest.mock('moduleName') .
Read more >
A guide to module mocking with Jest - Emma Goto
A guide to module mocking with Jest · Mocking a named import · Mocking only the named import (and leaving other imports unmocked)...
Read more >
Mocking ES and CommonJS modules with jest.mock() - Medium
Jest will automatically hoist jest.mock calls to the top of the module (before any imports). Since calls to jest.mock() are hoisted to the...
Read more >
How To Mock Dependencies With Jest
Instead of mocking every function, jest helps us mimic the entire module using jest.mock. Create mocks directory into the same path of the...
Read more >
jest-mock - npm
Note:** More details on user side API can be found in [Jest documentation](https://jestjs.io/docs/mock-function-api).
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