繁体   English   中英

开玩笑:TypeError:无法读取未定义的属性(读取“getFileName”)

[英]Jest: TypeError: Cannot read properties of undefined (reading 'getFileName')

我最近开始使用 @testing-library/user-event 库,但遇到了很多问题。 我正在尝试测试文本框验证流程,但出现错误

TypeError: Cannot read properties of undefined (reading 'getFileName')

      at _default (node_modules/@jest/source-map/build/getCallsite.js:86:60)
      at Function.write (node_modules/@jest/console/build/BufferedConsole.js:104:51)
      at console._logError (node_modules/@jest/console/build/CustomConsole.js:107:12)
      at console.error (node_modules/@jest/console/build/CustomConsole.js:147:10)

这是我的测试用例

  test('allows user to add comments in the comment Box', () => {
    render(
      <ApproveModal
        showApproveModal={showApproveModal}
        updateApproveModal={updateApproveModal}
        requestId={requestId}
        hierarchyId={hierarchyId}
      />
    );

    expect(screen.getByText('Enter few details before Approval')).toBeInTheDocument();

    const commentTextBox = screen.getByRole('textbox');
    userEvent.type(commentTextBox, 'Approved By Admin');

    expect(screen.queryByText('Please input a Valid Comment!')).not.toBeInTheDocument();

    userEvent.type(commentTextBox, '');

    // expect(screen.queryByText('Please input a Valid Comment!')).toBeInTheDocument();
  });

以前有人遇到过这个问题吗?

尝试使用const user = userEvent.setup()行实例化 userEvent class 并将现有的 userEvent 调用替换为 user.

这也可能对您有所帮助: https://testing-library.com/docs/user-event/setup

此外,最好一次只渲染一个组件而不是多个组件,然后尝试测试您在该特定组件上测试的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM