.toMatch(regexpOrString)

Use .toMatch to check that a string matches a regular expression.

For example, you might not know what exactly essayOnTheBestFlavor() returns, but you know it's a really long string, and the substring grapefruit should be in there somewhere. You can test this with:

  1. describe('an essay on the best flavor', () => {
  2. test('mentions grapefruit', () => {
  3. expect(essayOnTheBestFlavor()).toMatch(/grapefruit/);
  4. expect(essayOnTheBestFlavor()).toMatch(new RegExp('grapefruit'));
  5. });
  6. });

This matcher also accepts a string, which it will try to match:

  1. describe('grapefruits are healthy', () => {
  2. test('grapefruits are a fruit', () => {
  3. expect('grapefruits').toMatch('fruit');
  4. });
  5. });