Use mockReturnValue(...)
import {
getCallingCode,
hasSupport,
} from 'utils/countryCallingCode'
jest.mock('utils/countryCallingCode', () => ({
getCallingCode: jest.fn(),
hasSupport: jest.fn(),
}))
describe('...', () => {
it('...', () => {
getCallingCode.mockReturnValue(1)
hasSupport.mockReturnValue(false)
expect(...
})
it('...', () => {
getCallingCode.mockReturnValue(0)
hasSupport.mockReturnValue(true)
expect(...
})
})
Dealing with default export from that util:
import theUtil from 'utils/theUtil'
jest.mock('utils/theUtil', () => ({
__esModule: true,
default: jest.fn(),
}))
describe('...', () => {
it('...', () => {
theUtil.mockReturnValue('some value')
expect(...
})
it('...', () => {
theUtil.mockReturnValue('some other value')
expect(...
})
})
Typescript:
Use as jest.Mock
. eg:
...
(getCallingCode as jest.Mock).mockReturnValue(1)
...
(theUtil as jest.Mock).mockReturnValue('some value')
...
or, more cleanly:
import theUtil from 'utils/theUtil'
jest.mock('utils/theUtil', () => ({
__esModule: true,
default: jest.fn(),
}))
const mockTheUtil = theUtil as jest.Mock
describe('...', () => {
it('...', () => {
mockTheUtil.mockReturnValue('some value')
expect(...
})
})