javascript - Check that a string contains one of the substring from an array with Jest - Stack Overflow

expectedStrings = ['abc','def','ghi']stringToCheck = 'zyxabc

expectedStrings = [
'abc',
'def',
'ghi'
]
stringToCheck = 'zyxabc'

I want to check if stringToCheck contains one of the strings of expectedStrings with Jest. I have seen stringContaining and arrayContaining methods but I'm not sure how should I pose to make the test work.

I would like to use something close to this example :

describe('stringMatching in arrayContaining', () => {
  const expected = [
    expect.stringMatching(/^Alic/),
    expect.stringMatching(/^[BR]ob/),
  ];
  it('matches even if received contains additional elements', () => {
    expect(['Alicia', 'Roberto', 'Evelina']).toEqual(
      expect.arrayContaining(expected),
    );
  });
  it('does not match if received does not contain expected elements', () => {
    expect(['Roberto', 'Evelina']).not.toEqual(
      expect.arrayContaining(expected),
    );
  });
});
expectedStrings = [
'abc',
'def',
'ghi'
]
stringToCheck = 'zyxabc'

I want to check if stringToCheck contains one of the strings of expectedStrings with Jest. I have seen stringContaining and arrayContaining methods but I'm not sure how should I pose to make the test work.

I would like to use something close to this example :

describe('stringMatching in arrayContaining', () => {
  const expected = [
    expect.stringMatching(/^Alic/),
    expect.stringMatching(/^[BR]ob/),
  ];
  it('matches even if received contains additional elements', () => {
    expect(['Alicia', 'Roberto', 'Evelina']).toEqual(
      expect.arrayContaining(expected),
    );
  });
  it('does not match if received does not contain expected elements', () => {
    expect(['Roberto', 'Evelina']).not.toEqual(
      expect.arrayContaining(expected),
    );
  });
});
Share Improve this question edited Mar 11, 2019 at 16:46 Quentin Del asked Mar 11, 2019 at 16:31 Quentin DelQuentin Del 1,6953 gold badges21 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I don't think bining arrayContaining and stringContaining is the best and most readable approach here (If even possible at all).

Instead simply try this method:

    const numberOfMatches = expectedStrings.filter(x => stringToCheck.indexOf(x) > -1).length;
    expect(numberOfMatches).toBeGreaterThan(0);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744288398a4566912.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信