我如何修复错误类型:无法读取未定义的属性(读取' push)'在React测试中?

发布于 2025-01-23 01:21:00 字数 1068 浏览 2 评论 0原文

通过反应测试图谱进行了测试。我正在实施一个测试,该测试在测试组件的过程中单击按钮时移至主页,但它不起作用。我该如何解决?

我有这个错误 typeError:无法读取未定义的属性(阅读'push')

//Error.test.tsx

test('메인페이지로 이동하는가?', async () => {
    const history = createMemoryHistory()
    render(
      <Router location={history}>
        <ErrorCompnent />
      </Router>,
    )
    const user = userEvent.setup()
    screen.getByTestId('goHome')
    await user.click(screen.getByTestId('goHome'))
    // expect(screen.getByTestId('main')).toBeInTheDocument()
  })
//Error Component
<button
          data-testid="goHome"
          onClick={() => {
            navigate('/')
          }}
        >
          메인으로 이동
        </button>
...
//Main.tsx
<article className={styles.container}>
      <section data-testid="main" className={styles.main}>
        <div className={styles.column}>
          <Calculate />
          <PrivateInfo />
        </div>
      </section>
...

Testing is in progress through react-testing-library. I'm implementing a test that moves to the main page when a button is clicked in the process of testing a component, but it doesn't work. How can I solve this?

i got this error
TypeError: Cannot read properties of undefined (reading 'push')

//Error.test.tsx

test('메인페이지로 이동하는가?', async () => {
    const history = createMemoryHistory()
    render(
      <Router location={history}>
        <ErrorCompnent />
      </Router>,
    )
    const user = userEvent.setup()
    screen.getByTestId('goHome')
    await user.click(screen.getByTestId('goHome'))
    // expect(screen.getByTestId('main')).toBeInTheDocument()
  })
//Error Component
<button
          data-testid="goHome"
          onClick={() => {
            navigate('/')
          }}
        >
          메인으로 이동
        </button>
...
//Main.tsx
<article className={styles.container}>
      <section data-testid="main" className={styles.main}>
        <div className={styles.column}>
          <Calculate />
          <PrivateInfo />
        </div>
      </section>
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文