Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Neither toThrow, nor toStrictEqual check additional properties of custom errors #15378

Open
todor-a opened this issue Nov 13, 2024 · 0 comments

Comments

@todor-a
Copy link

todor-a commented Nov 13, 2024

Version

29.7.0

Steps to reproduce

export class HttpError extends Error {
  constructor(public readonly code?: string) {
    super();

    this.code = code;
    this.name = 'HttpError';
    this.message = this.name;

    Error.captureStackTrace(this, HttpError);
  }
}

export class BadRequestError extends HttpError {
  constructor(public readonly code?: string) {
    super(code);
  }
}


async function handler() {
  throw new BadRequestError('foo');
}

it('should throw correct error', async () => {
  // should reject but does not
  await expect(() => handler()).rejects.toThrow(
    new BadRequestError('bar' )
  );
  await expect(() => handler()).rejects.toStrictEqual(
    new BadRequestError('bar')
  );
});

Stackbliz.

Expected behavior

Either of these to fail the test.

Actual behavior

Neither does.

Additional context

I've read that toThrow checks only the message property. How to check all properties? There is a third party package but I feel like this should be native to jest.

Environment

This is reproducible in the stackblitz.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant