【问题标题】:Retrieve location.href from jest with jsdom使用 jsdom 从 jest 中检索 location.href
【发布时间】:2018-08-30 06:49:42
【问题描述】:

我正在尝试在 Jest 中使用 jsdom 来检索路径名。到目前为止我找到了这个post,其中testURL设置在package.json

  "jest": {
    "testURL": "http://localhost:3000/"
  }

据我所知,我不需要 jsdom 设置文件。 所以我在测试中就这样使用它:

import { jsdom, location } from 'jsdom';

  it('directs to /question', () => {
  const handleClick = jest.fn();
    const button = shallow(
      <Button handleClick={handleClick} />
    );
    button.find('button').simulate('click');
    expect(location.href).toBe("/question")
  });

当我这样做时,我总是收到错误Cannot read property 'href' of undefined

我该如何解决这个问题?

【问题讨论】:

    标签: reactjs jestjs jsdom


    【解决方案1】:

    尽量不要从jsdom导入location。 Jest 有它自己的 location 对象。

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 2017-06-02
      • 2019-06-01
      • 2016-04-27
      • 1970-01-01
      • 2017-06-25
      • 2021-10-05
      • 2018-03-30
      • 2022-11-10
      相关资源
      最近更新 更多