Categories
JavaScript Answers

How to check if a string contains ‘abc’ or ‘cde’ with Jest?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *