期望在笑话中包含来自一组值的文本,用 React 酶

Expect toContain text from a set of values in jest,enzyme with React

我的以下测试有效,

let output = mount(story);
      expect(output.text()).toContain('Superman');

但我什至需要让蝙蝠侠和蜘蛛侠通过: 我需要检查 output.text() 是否有 -> ['Superman','Batman','Spiderman']

无法使用

expect(output.text()).toContain(['Superman','Batman','Spiderman']);

output.text() 将包含 "Superman is the best" 或 "Spiderman is the best"

You can add your own matcher 而不是 toContain.

expect.extend({
  toContainHero(text) {
    let pass = false;
    const heroes = ['Superman','Batman','Spiderman'];
    heroes.forEach((hero) => {
      pass = text.indexOf(hero) !== -1;
    })
    if (pass) {
      return {
        message: () =>
          `expected hero to be found`,
        pass: true,
      };
    } else {
      return {
        message: () => `expected hero to be not found`,
        pass: false,
      };
    }
  },
});

然后做:

expect(output.text()).toContainHero();

像这样的答案更适合您:

expect(output.text()).toEqual(expect.stringMatching(/^(Batman|Superman|Spiderman)/));