Sometimes, we want to check if a string contains ‘abc’ or ‘cde’ with Jest.
In this article, we’ll look at how to check if a string contains ‘abc’ or ‘cde’ with Jest.
How to check if a string contains ‘abc’ or ‘cde’ with Jest?
To check if a string contains ‘abc’ or ‘cde’ with Jest, we can call toMatch
with a regex that matches both strings.
For instance, we write
expect(str).toMatch(/(abc|cde)/i)
to check if str
is 'abc'
or 'cde'
in a case-insensitive manner.
The i
flag lets us check for str
in a case-insensitive manner.
Conclusion
To check if a string contains ‘abc’ or ‘cde’ with Jest, we can call toMatch
with a regex that matches both strings.