Last active
July 27, 2018 00:03
-
-
Save ThaddeusJiang/1de29a7007f5792778d40b0216e7fe73 to your computer and use it in GitHub Desktop.
jest mock function return 无参
This file contains hidden or 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
const mock = jest.fn(); | |
mock.mockReturnValue(42); | |
mock(); // 42 | |
mock.mockReturnValue(43); | |
mock(); // 43 | |
// 或者 | |
const myMockFn = jest | |
.fn() | |
.mockReturnValue('default') | |
.mockReturnValueOnce('first call') | |
.mockReturnValueOnce('second call'); | |
// 'first call', 'second call', 'default', 'default' | |
console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
如果 function 返回结果是根据输入计算出来的,使用
mockFn.mockImplementation(() => { })