Last active
July 22, 2021 06:01
-
-
Save alvinncx/a710b690ac49a37241ae53e78c87ddac to your computer and use it in GitHub Desktop.
Mocking Typescript named export modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const function1 = () => { | |
return 'awesome' | |
} | |
export const function2 = () => { | |
return 'not so awesome' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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