[英]Unsafe member access [0] on an `any` value
I'm trying to assert the parameters my payment confirmation submission function is called with, but getting a type error on confirmPaymentMock.mock.calls[0][0]
:我正在尝试断言调用我的付款确认提交 function 的参数,但在
confirmPaymentMock.mock.calls[0][0]
上收到类型错误:
const confirmPaymentMock = jest.fn();
it('should call onSubmit on button click', () => {
const { getByTestId } = renderComponent('orderId');
fireEvent.click(getByTestId('submitPay'));
//Unsafe member access [0] on an `any` value.
expect(JSON.stringify(confirmPaymentMock.mock.calls[0][0])).toEqual(JSON.stringify({
elements: mockStripe().elements(),
confirmParams: {
return_url: `${window.location.origin}/${appRoutes.successfulOrder}`
}
}));
});
Type safety works via static code analysis and confirmPaymentMock.mock.calls[0][0]
will only be valid if confirmPaymentMock
is called at least once, which is not something that can always be determined without running the code.类型安全通过 static 代码分析起作用,并且
confirmPaymentMock.mock.calls[0][0]
只有在至少调用一次confirmPaymentMock
时才有效,这不是在不运行代码的情况下总能确定的事情。 So what options are there for dealing with this?那么有什么选择可以解决这个问题呢?
You can ignore the error.您可以忽略该错误。 You can use a safer access method.
您可以使用更安全的访问方法。 Or you can add extra checks before this line to ensure you only access a safe field.
或者您可以在此行之前添加额外的检查以确保您只访问安全字段。
Ignoring the error is quite straightforward, you add a directive for ESLint to ignore that specific error on that line.忽略错误非常简单,您可以为 ESLint 添加指令以忽略该行上的特定错误。
A safer access method would be something like lodash get , which will return a default value if the path is invalid.更安全的访问方法类似于lodash get ,如果路径无效,它将返回默认值。
An extra check would involve using an additional expect statement to make sure confirmPaymentMock.mock.calls[0]
is not null.额外的检查将涉及使用额外的 expect 语句来确保
confirmPaymentMock.mock.calls[0]
不是 null。
The last approach here is, in my opinion, the safest one to take.在我看来,这里的最后一种方法是最安全的方法。 Your unit test should not just pass with the current state of the code, it should either remain passing or give a meaningful error if the code is changed.
您的单元测试不应仅通过当前代码的 state,它应该保持通过或在代码更改时给出有意义的错误。 That means if the code being tested is refactored so that your mock is not called any more, you'd prefer the test to fail with an error along the lines of
Expected confirmPaymentMock.mock.calls[0] to not be null
to an error like TypeError: Cannot read property of undefined
, which is much less readable.这意味着如果被测试的代码被重构,这样你的 mock 就不再被调用,你更希望测试失败并出现
Expected confirmPaymentMock.mock.calls[0] to not be null
to an error像TypeError: Cannot read property of undefined
,它的可读性要差得多。
You can also get a useful error message along these lines if you use _.get(confirmPaymentMock, 'mock.calls[0][0]', 'Payment mock was not called')
as then the expect statement making the comparison will tell you that there is a mismatch, and it expected some real value but got 'Payment mock was not called'.如果您使用
_.get(confirmPaymentMock, 'mock.calls[0][0]', 'Payment mock was not called')
,您还可以沿着这些行获得有用的错误消息,因为进行比较的期望语句会告诉你认为存在不匹配,它期望一些实际值但得到“未调用付款模拟”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.