1 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
只需將 分配給handler外部定義的變量。
例如
describe('61504714', () => {
it('should pass', () => {
let handlerRef;
const check = jest.fn().mockImplementation((_, handler) => {
console.log('call check');
handlerRef = handler;
});
const callback = jest.fn((yesOrNo: boolean) => (yesOrNo ? 'yes' : 'no'));
check('_', callback);
expect(callback).not.toBeCalled();
const rval = handlerRef(true);
expect(rval).toBe('yes');
expect(check).toBeCalledWith('_', callback);
expect(callback).toBeCalledWith(true);
});
});
試驗(yàn)結(jié)果:
PASS stackoverflow/61504714/index.test.ts (11.463s)
61504714
? should pass (33ms)
console.log stackoverflow/61504714/index.test.ts:5
call check
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 13.581s
添加回答
舉報(bào)