1 回答
TA贡献1824条经验 获得超5个赞
只需将 分配给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);
});
});
试验结果:
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
添加回答
举报