question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Error Exceptions properties are not evaluated for strict equality

See original GitHub issue

🐛 Bug Report

Custom properties on objects constructed from a Class that extends Error are not evaluated when matched with either .toThrow or .toThrowError.

To Reproduce

  1. Create a custom class that extends the Error object.
Class CustomError extends Error {
  constructor(message, code) {
    super(message);
    this.code = code;
  }
}
  1. Create a custom error object using class constructor.
const customError = new CustomError('something wrong happened', 'ERR_CUSTOM_CODE');
  1. In the code that needs to be tested, throw that custom error.
function fnThatThrowNoMatterWhat() {
   throw customError;
}
  1. In the test, try to pass a new instance of that custom error object (created in the test file) and pass it to .toThrowError.
const expectedError = new CustomError('something wrong happened', 'WRONG_ERR_CUSTOM_CODE');
 expect(() => main()).toThrowError(expectedError); // pass when it should fail

Expected behavior

All properties added to the object are checked strictly and not just message.

Acceptance criteria:

Link to repl or repo (highly encouraged)

repl.it example

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver)
    CPU: x64 Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
  Binaries:
    Node: 8.10.0 - ~/.nvm/versions/node/v8.10.0/bin/node
    Yarn: 1.9.2 - /usr/bin/yarn
    npm: 6.3.0 - ~/.nvm/versions/node/v8.10.0/bin/npm

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:5

github_iconTop GitHub Comments

2reactions
runarbergcommented, Apr 16, 2019

Note:

  • AVA’s .throws returns the error object so you can do arbitrary tests on it.
  • Chai’s .throw expectation can be chained with other assertions like .with.property('code', 42).

Here is a stack overflow answer for overcoming this limitation of jest.

0reactions
github-actions[bot]commented, Apr 30, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strict equality (===) - JavaScript - MDN Web Docs
The strict equality (===) operator checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality...
Read more >
How do I create a custom Error in JavaScript? - Stack Overflow
This confirmes the "problem" I ran into was the stack property of the error was the line number where new Error() was created,...
Read more >
JavaScript Custom Errors, Extending Error - W3docs
In this chapter, we are going to dive into more details about errors in JavaScript. Learn what custom errors and extending error are...
Read more >
Improving TypeScript error handling with exhaustive type ...
Discover an improved method for handling errors in TypeScript that solves problems that arise from returning null and throwing try...catch.
Read more >
Custom errors, extending Error - The Modern JavaScript Tutorial
Low-level exceptions sometimes become properties of that object like err.cause in the examples above, but that's not strictly required. Tasks ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found