简体   繁体   English

对“任何”值的不安全成员访问 [0]

[英]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.

相关问题 收到此打字稿错误:对任何值不安全的成员访问 [key] - Getting this typescript error: Unsafe member access [key] on an any value 如何使用 cloneElement 修复“不安全成员访问”反应 typescript 错误? - How to fix 'unsafe-member-access' react typescript error with cloneElement? 将 react-transition-group 与 TypeScript 一起使用时出现“任何值的不安全分配” - "Unsafe assignment of an any value" when using react-transition-group with TypeScript 覆盖类型以防止不安全的任何调用 - Overriding types to prevent unsafe any calls Typescript React 输入 onchange 不安全赋值 'any' - Typescript React input onchange unsafe assignment 'any' UNSAFE_componentWillReceiveProps 总是触发每次输入值更改 - UNSAFE_componentWillReceiveProps always triggering every input value change 将URL哈希参数值直接分配给Javascript变量是否不安全? - Is it unsafe to directly assign a URL hash parameter value to a Javascript variable? 如何在项目中全局保存任何值,以便我们可以从react.js中的任何位置访问它 - How to save any value globally in the project so that we can access it from anywhere in react.js Type脚本中的React组件生命周期方法的成员访问 - member access for react component lifecycle methods in typescript 在javascript中访问类型为object的数组成员 - access array member of type object in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM