-
Notifications
You must be signed in to change notification settings - Fork 112
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
Custom error assertion #113
Comments
This is indeed an area where Chai is not perfectly expressive (and thus Chai as Promised, which follows its API, is similar). This is the best I have: it("should fail", function () {
return theFunctionToTest().then(
function () {
assert.fail("Expected to fail.");
},
function (e) {
expect(e).to.be.an.instanceof(MyError);
expect(e.message).to.equal("Error message");
expect(e.foo).to.deep.equal(["foo"]);
}
);
}); If you'd like something more elegant, what you need to do is ask Chai for a similar function. E.g. something like it("should fail sync", function () {
expect(theFunctionToTest).to.throwMatching(function (e) {
expect(e).to.be.an.instanceof(MyError);
expect(e.message).to.equal("Error message");
expect(e.foo).to.deep.equal(["foo"]);
});
}); which would allow you to eliminate the |
chaijs/chai#470 is a PR to implement breaking out the error checking components of chai into their own lib, which could theoretically be used to build up a |
@keithamus Thanks for showing the way to implement a |
In essence with the checkError functionality, you could write something like: expect(theFunctionToTest).to.throwMatching(/Error message/);
expect(theFunctionToTest).to.throwMatching(new MyError('Error message'));
expect(theFunctionToTest).to.throwMatching(MyError); |
@keithamus I don't get the point of your last post.. Chai.js already have those functionalities via expect(theFunctionToTest).to.throw(/Error message/);
expect(theFunctionToTest).to.throw(new MyError('Error message'));
expect(theFunctionToTest).to.throw(MyError); And Chai as promised already support those via return expect(theFunctionToTest()).to.be.rejectedWith(/Error message/);
return expect(theFunctionToTest()).to.be.rejectedWith(new MyError('Error message'));
return expect(theFunctionToTest()).to.be.rejectedWith(MyError); The only thing we need is something like Example of requested features for Chai.js: expect(theFunctionToTest).to.throwMatching(function (e) {
expect(e).to.be.an.instanceof(MyError);
expect(e.message).to.equal("Error message");
expect(e.foo).to.deep.equal(["foo"]);
}); or (?) expect(theFunctionToTest).to.throwMatching(MyError, "Error Message", function (e) {
// Some advanced assertions here...
expect(e.foo).to.deep.equal(["foo"]);
}); That will become something like that for Chai as Promised: return expect(theFunctionToTest()).to.be.rejectedWith(function (e) {
expect(e).to.be.an.instanceof(MyError);
expect(e.message).to.equal("Error message");
expect(e.foo).to.deep.equal(["foo"]);
}); or (?) return expect(theFunctionToTest()).to.be.rejectedWith(MyError, "Error Message",
function (e) {
// Some advanced assertions here...
expect(e.foo).to.deep.equal(["foo"]);
}
); |
Ah I think I see what you want. In which case, my comments don't really contribute here. We have Also, chai's sync expect(throwingFunction).to.throw(MyError).and.have.property('foo').deep.equal(['foo']); Theoretically there's nothing stopping you doing the same within Chai-As-Promised, as it should convert all assertions to run in the promise chain, so something like: expect(theFunctionToTest()).to.be.rejectedWith(MyError).and.eventually.have.property('foo').deep.equal(['foo']); However, I'm not sure this would work as it seems |
Oooh, I did not know that about .throw. Seems like a good thing for .rejected and .rejectedWith to do. |
You've probably figured this; but it could well be a breaking change - as any existing code that chains off of |
Was chaining it ever supported? If it would still work with the |
@yvele I think you could probably chain your custom error messages with the expect(somePromise).to.eventually.be.rejected.and.deep.equal({
data: expectedData,
errorMessage: expectedMessage
}).notify(done); It might work even without the |
I'm rolling up all bugs in the category "isRejected/rejectedWith doesn't behave like Chai's error rejectors" into #166. |
- `.fulfilled`, `.not.rejected` and `.not.rejectedWith` when successful: * The promise they return is fulfilled with the same value as the promise they were testing. - When `.not.fulfilled`, `.rejected` and `.rejectedWith` are successful: * The promise they return is fulfilled with the rejection reason. In all cases, when they fail, the promise they return is rejected and has for reason the assertion that failed. The changes above make it so that the `object` flag is automatically set when the promise returned is resolved, and thus also allows chaining. This fixes chaijs#113.
- `.fulfilled`, `.not.rejected` and `.not.rejectedWith` when successful: the promise they return is fulfilled with the same value as the promise they were testing. - When `.not.fulfilled`, `.rejected` and `.rejectedWith` are successful: The promise they return is fulfilled with the rejection reason. In all cases, when they fail, the promise they return is rejected and has for reason the assertion that failed. The changes above make it so that the `object` flag is automatically set when the promise returned is resolved, and thus also allows chaining. This fixes #113.
I would like to achieve the following with Chai expressivity:
This works fine, but it lacks a way to make custom error assertions:
Because of any assertion made in
then
will make the promise chain fail, you can't do something like that:Is there a way to custom assert my error?
If not, can we add a new feature? Like passing an assertion function to the
rejectedWith
function?Let me know what do you think about that..
The text was updated successfully, but these errors were encountered: