Skip to content

Instantly share code, notes, and snippets.

@alvinncx
Last active July 22, 2021 06:01
Show Gist options
  • Save alvinncx/a710b690ac49a37241ae53e78c87ddac to your computer and use it in GitHub Desktop.
Save alvinncx/a710b690ac49a37241ae53e78c87ddac to your computer and use it in GitHub Desktop.
Mocking Typescript named export modules
export const function1 = () => {
return 'awesome'
}
export const function2 = () => {
return 'not so awesome'
}
/*
This could be a test where myModule is dependent on.
Example shows how you can mock only one of the function among all the
exported functions in your module.
*/
import { function1 } from './myModule'
import { mocked } from 'ts-jest'
const mockedFunction1 = mocked(function1) // needed to get the types correct
jest.mock('./myModule', () => {
return {
...(jest.requireActual('../../src/utils/commonUtil') as Record<string,any>),
function1: jest.fn()
}
})
it('example 1', () => {
mockedFunction1.mockImplementation(() => 'who cares')
// your assertions here
})
it('example 2', () => {
mockedFunction1.mockImplementation(() => 'can be anything')
// your assertions here
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment