Jest: expect(…).rejects.toMatch(…) should handle an Error object

Created on 31 May 2017  Â·  3Comments  Â·  Source: facebook/jest

The current documentation contains the following example:

test('fetchData() rejects to be error', async () => {
  const drinkOctopus = new Promise(() => {
      throw new DisgustingFlavorError('yuck, octopus flavor');
  });

  await expect(drinkOctopus).rejects.toMatch('octopus');
});

Simplified slightly, this would be expected to pass:

  it('throws an error', async () => {
    const drinkOctopus = new Promise(() => {
      throw new Error('yuck, octopus flavor');
    });

    await expect(drinkOctopus).rejects.toMatch('octopus');
  });

Running this test in Jest v20.0.4 (node v8.0.0) produces an error:

Failed: expect(string)[.not].toMatch(expected)

string value must be a string.
Received:
  object: [Error: yuck, octopus flavor]

The Promise is rejecting with an Error rather than a string, as it should, but Jest isn't handling this as expected.

Most helpful comment

I wouldn't close this; while the solution is being discussed on #3601, I think a quick documentation change is needed ATM so this issue seems fit for that as it looks like not even a solution is proposed yet in that issue.

I think developers using the documentation should not be expected to find an issue in Github that tells them that the docs are wrong. So I propose to reopen this issue as: "Update the documentation to reflect current status of things".

All 3 comments

We also discuss about this in https://github.com/facebook/jest/issues/3601 so it might be a dupe.

Yeah it seems like a dupe... Closing in favor of #3601

I wouldn't close this; while the solution is being discussed on #3601, I think a quick documentation change is needed ATM so this issue seems fit for that as it looks like not even a solution is proposed yet in that issue.

I think developers using the documentation should not be expected to find an issue in Github that tells them that the docs are wrong. So I propose to reopen this issue as: "Update the documentation to reflect current status of things".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TYRONEMICHAEL picture TYRONEMICHAEL  Â·  80Comments

paularmstrong picture paularmstrong  Â·  66Comments

pfftdammitchris picture pfftdammitchris  Â·  76Comments

sterpe picture sterpe  Â·  70Comments

seibelj picture seibelj  Â·  116Comments