Skip to content

toThrow does not behave like toEqual for non-errors #7692

@rubennorte

Description

@rubennorte

🐛 Bug Report

It seems like last changes in toThrow (#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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions