我想测试一个函数,它只返回一个带有一些值的函数和一个匿名函数作为参数。如何在玩笑中使用 toHaveBeenCalledWith 测试匿名函数的匹配?
function toBeTested( id, values) {
return xyz(id, values, () => {
return {
type: 'foo',
payload: {
text: values.text
}
}
})
}
在我的测试中
describe('test for toBeTested', () => {
it('should call xyz with params', () => {
const id = 123;
const values = {
text: 'Hello world',
};
xyz = jest.fn();
toBeTested(id, values);
expect(xyz).toHaveBeenCalledWith(id, values, () => {
return {
type: 'foo',
payload: {
text: values.text,
}
}
});
})
})
测试错误报告
expect(jest.fn()).toHaveBeenCalledWith(expected)
Expected mock function to have been called with:
[123, {text: 'Hello world'}, [Function anonymous]]
But it was called with:
[123, {text: 'Hello world'}, [Function anonymous]]
at Object.it.only (src/actions/tests/xyz.test.js:30:43)
at new Promise (<anonymous>)
at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)