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.

Make the jest mock typed

See original GitHub issue

Describe the Feature

The jest mock is in javascript so you get warnings in VSCode that it is not typed.

Possible Implementations

I have implemented my mock in __mocks __ as follows

import type NetInfo from '@react-native-community/netinfo';
import type {
  NetInfoConfiguration,
  NetInfoState
} from '@react-native-community/netinfo';

export enum NetInfoStateType {
  unknown = 'unknown',
  none = 'none',
  cellular = 'cellular',
  wifi = 'wifi',
  bluetooth = 'bluetooth',
  ethernet = 'ethernet',
  wimax = 'wimax',
  vpn = 'vpn',
  other = 'other',
}

const connectedState: NetInfoState = {
  type: NetInfoStateType.wifi,
  isConnected: true,
  isInternetReachable: true,
  details: {
    ssid: null,
    bssid: null,
    strength: null,
    ipAddress: null,
    subnet: null,
    frequency: null,
    isConnectionExpensive: false,
    linkSpeed: null,
    rxLinkSpeed: null,
    txLinkSpeed: null,
  },
};

export default <typeof NetInfo>{
  fetch: jest.fn(() => Promise.resolve(connectedState)),
  refresh: jest.fn(() => Promise.resolve(connectedState)),
  configure: jest.fn((_configuration: Partial<NetInfoConfiguration>) => {}),
  addEventListener: jest.fn(() => jest.fn()),
  useNetInfo: jest.fn(() => connectedState),
};

Related Issues

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mikehardycommented, Dec 16, 2022

Hi there, we merge reasonable PRs.

0reactions
trajanocommented, Dec 17, 2022

I’m trying to see if someone can answer https://stackoverflow.com/questions/74828607/how-do-i-add-extra-functions-to-a-mock-and-have-it-pass-typescript so we can add some extra functions to at least do some netinfostate changes for testing. But I’ll remove the code for now.

The mock https://github.com/trajano/spring-cloud-demo/blob/dbcfbc1be7e75d2cc09c8dce08fd976c07874d03/packages/auth-context/src/__mocks__/@react-native-community/netinfo.ts

The test https://github.com/trajano/spring-cloud-demo/blob/dbcfbc1be7e75d2cc09c8dce08fd976c07874d03/packages/auth-context/src/__tests__/netinfoMock.test.tsx

This adds a bit more ways to test on jest. I have another one in a future commit that also adds a way of firing through listeners but right now the way to activate it doesn’t look nice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to strongly type jest mocks - Stack Overflow
You can use jest.fn() to mock instantiating the class object and omit specifying the type on the ...
Read more >
Mock Functions - Jest
You can create a mock function with jest.fn(). ... Each entry in this array is an object containing a type property, and a...
Read more >
Typed mocks for Jest - HipsterBrown
TL:DR jest-when and its associated types package are useful additions to any TypeScript codebase looking for a nicer way to use Jest mocks....
Read more >
Understanding Jest Mocks - Medium
Mocking is a technique to isolate test subjects by replacing dependencies with objects that you can control and inspect.
Read more >
How to use TypeScript and Jest mocks - Breno Calazans
It's pretty common to mock modules in Jest. When using TypeScript that might be a bit harder because they are not automatically resolved...
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