Jest: toThrow does not behave like toEqual for non-errors

Created on 24 Jan 2019  路  2Comments  路  Source: facebook/jest

馃悰 Bug Report

It seems like last changes in toThrow (https://github.com/facebook/jest/pull/7621) broke a behaviour uses were relying on in Jest 23.

We can expect toThrow to behave like toEqual on the thrown error, but that's not the case anymore.

To Reproduce

Steps to reproduce the behavior:

Run this test with Jest 23 and Jest 24.0.0-alpha.13:

class CustomError { // not extending Error!
  constructor(message) {
    this.message = message;
  }
}

function throwError() {
  throw new CustomError('foo');
}

test('toThrow', () => {
  expect(throwError).toThrowError(new CustomError('foo'));
});

test('toEqual', () => {
  try {
    throwError();
  } catch (e) {
    expect(e).toEqual(new CustomError('foo'));
    return;
  }

  throw new Error('Expected error here');
});

Expected behavior

No errors. Those two tests should be equivalent.

Also, I found the error message a bit confusing:

screen shot 2019-01-24 at 10 45 23

Bug

Most helpful comment

(before anyone picks this up, @pedrottimark is working on it)

All 2 comments

(before anyone picks this up, @pedrottimark is working on it)

I could repro this asymmetry with non-errors, but there might be more cases like that.

Was this page helpful?
0 / 5 - 0 ratings