Make the jest mock typed
See original GitHub issueDescribe 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:
- Created 9 months ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi there, we merge reasonable PRs.
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.