skip to Main Content

I am trying to traverse through a list of elements and want to assert that the body includes either of the 2 values.
I am trying to use the following but it only lets me assert one value.

expect(cellText).includes(toDateFormat);

How can I make an assertion that allows me to assert multiple values having OR condition?

2

Answers


  1. You could use Chai’s satisfy to accomplish this.

    Invokes the given matcher function with the target being passed as the first argument, and asserts that the value returned is truthy.

    expect(cellText).to.satisfy(function(text) {
      return text.includes(toDateFormat) || text.includes(otherFormatVariable);
    })
    

    If you have a long list of potential expected values, you could simplify this by having those values in an array, and using array.filter() to find instances of the actual text value in the expected text value, and returning the length of the filtered array.

    const expected = ['foo', 'bar', 'baz']
    expect(cellText).to.satisfy(function(text) {
      return expected.filter((x) => cellText.includes(x)).length
    });
    
    Login or Signup to reply.
  2. A simple way is to use oneOf assertion

    Asserts that the target is a member of the given array list.

    expect(cellText).to.be.oneOf([format1, format2])
    

    or for partial formats

    expect(cellText).to.contain.oneOf([format1, format2])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search