Cannot handle thrown if not derived from Error
See original GitHub issueimport { expect } from "chai";
class Exception {}
describe("chai", () => {
it("should handle thrown object", () => {
expect(()=> {
throw new Exception();
}).to.throw(Exception);
});
});
- chai should handle thrown object: AssertionError: expected [Function] to throw ‘Exception’ but {} was thrown
currently have to wrap as follows:
import { expect } from "chai";
class SentinelNoError {}
const sentinelNoError = new SentinelNoError();
function returnCatch(fn: ()=>void): any {
try {
fn();
return sentinelNoError;
} catch(error) {
return error;
}
}
class Exception {}
describe("chai", () => {
it("should handle thrown object", () => {
expect(returnCatch(()=> {
throw new Exception();
})).to.be.instanceof(Exception);
});
});
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Exceptions and Error Handling, C++ FAQ - Standard C++
As a rule of thumb, exception handling is extremely cheap when you don't throw an exception. It costs nothing on some implementations. All...
Read more >Correct Exceptions in C++ - Stack Overflow
First, exceptions let you segregate the code that deals with errors, so the main idea and readability of the code don't get lost...
Read more >Top 15 C++ Exception handling mistakes and how to avoid ...
Mistake # 6: Not Throwing an exception by value ... If you throw a pointer to an exception object, you'll need to deal...
Read more >Modern C++ best practices for exceptions and error handling
In modern C++, in most scenarios, the preferred way to report and handle both logic errors and runtime errors is to use exceptions....
Read more >C++ Tutorial: Handling Exceptions - 2020 - BogoToBogo
If a program wants to open a file, it is not a normal flow, because the user ... But we can signal an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jseter The ability to provide arguments inside of the
.throw
assertion is just meant as a shortcut for the most common use cases, allowing a user to perform multiple assertions internally on the thrown value with a single assertion. It’s not meant to cover every possibility. The more possibilities it covers, the more complex (for Chai) and troublesome (for users) the code becomes, as described here.Although this branch hasn’t been merged into Chai yet, the approach documented here should work for you in the current version of Chai.
@liuxh0 The good news is that using the
.throw()
assertion without any parameters causes it to only care about whether or not the function threw. It doesn’t care if anError
object was thrown, or some other object, or a string, or evenundefined
. And you can chain any of Chai’s other assertions (e.g.,instanceof
) afterward to make assertions on the thrown value.The behavior of
.throw
when one or more parameters are passed isn’t quite right, I think. It’s likely that the solution I worked on last year isn’t perfect either. It’s a fair point that.throw
shouldn’t care what type of value is thrown, even when passing parameters to.throw
; otherwise, as you said, it’d be more like.throwError
in that case.