【发布时间】:2021-12-15 14:04:12
【问题描述】:
给定一个带有签名的函数
interface Position {
x: number, y: number
}
function f(position: Position): void { … }
我想在测试中模拟它
// given
const f = jest.fn()
// when
// exercise SUT
// then
expect(f).toHaveBeenCalledWith<Position>({x: 1.0, y: 1.0});
但不幸的是,我正在处理浮点数,所以我需要检查以留出一些余地。现在,我可以做
expect(f.mock.calls[0][0].x).toBeCloseTo(1, 1);
expect(f.mock.calls[0][0].y).toBeCloseTo(1, 1);
但这会很快变老真实。 Jest 维护者don't seem to think warrants inclusion in Jest 有一张票,match using expect 有一个包,但它没有做我想做的事!类似于:
expect(f).toHaveBeenCalledWith(
expect.objectContaining({
x: expect.numberCloseTo(1),
y: expect.numberCloseTo(1)
});
有类似的东西还是我必须自己动手?
【问题讨论】:
-
不,没有内置的,但你可以添加自己的
expect.extend
标签: javascript typescript jestjs