【问题标题】:accessing props().x returns undefined访问 props().x 返回 undefined
【发布时间】:2018-11-08 00:11:32
【问题描述】:

我正在使用 create-react-app 打字稿启动器,我注意到道具不起作用。我是不是做错了什么?

import * as enzyme from "enzyme";
import * as React from "react";
import Box from "./Box";

describe("Box component", () => {
  let wrapper: any;

  beforeEach(() => {
    wrapper = enzyme.shallow(<Box x={0} y={2} />);
  });

  it("should not render without x", () => {
    expect(wrapper.props().x).toBe(0);
  });
});

这就是我遇到的错误。

expect(received).toBe(expected) // Object.is equality

    Expected value to be:
      0
    Received:
      undefined

    Difference:

【问题讨论】:

  • 你试过wrapper.instance().propsenzyme.mount(...)吗?
  • @AlexandreAnnic 谢谢,刚刚尝试并收到此错误:TypeError: Cannot read property 'props' of null
  • @AlexandreAnnic 呵呵,它似乎只在 mount() 时有效??
  • 酶对我来说也是一个谜:-)。也许检查gist.github.com/fokusferit/e4558d384e4e9cab95d04e5f35d4f913

标签: javascript reactjs typescript jestjs enzyme


【解决方案1】:
it("should not render without x", () => {
    expect(wrapper.prop('x')).toBe(0);
  });

使用 prop() 代替 props()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 2019-02-06
    • 2019-04-28
    • 2021-07-31
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多