1

我在 React.js 中有一个简单的 Intro 组件,它呈现一个 h1 和一个 p。

我正在尝试使用 Enzyme 为通过的 h1 & p 字符串编写测试,但我无法做到这一点。这段代码有什么问题?

it('description of element is okay <p>', () => {
  const wrapper = shallow(<Intro title="Heading of element" description="Description of element" />);
  expect(wrapper.find('p').text().to.contain("Description of element")); // This is not working!
});

如果我控制台记录wrapper.find('p').text()它不是未定义的......但控制台说这样的:

  1) (Component) Intro contains a single <p>:
 TypeError: Cannot read property 'contain' of undefined
  at Context.<anonymous> (test/components/alerts/alert.spec.js:19:12)
4

3 回答 3

3

最可能的断言应该如下所示:

expect(wrapper.find('p').text()).to.contain('Description of element')

expect像这样工作:

expect(something).to.do.something
于 2016-08-04T09:51:14.390 回答
3

检查字符串内容的新 API 是

expect(wrapper.find('p').text()).toContain('text to check')

如需参考,请查看jest toContain 文档

于 2020-09-27T12:19:19.843 回答
0

您正在调用.to.contain酶包装,而不是期望对象。所以代替这个:

expect(wrapper.find('p').text().to.contain("Description of element"));

你应该有:

expect(wrapper.find('p').text()).to.contain("Description of element");
于 2016-08-04T09:51:39.157 回答