-
-
Notifications
You must be signed in to change notification settings - Fork 702
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
adding checkError utility to compare errors against criteria #470
Conversation
Hi guys, does this look like it could work? Happy to make any changes as necessary. |
Hi @vikki sorry for not looking at this yet. I'll take a look in the next couple of days and leave some proper feedback. Feel free to pester me if I haven't by then 😄 |
Okay @vikki, some general comments to start us off with this PR:
Here is what I imagine function assertThrows (constructor, errorLike, msg) {
if (msg) {
flag(this, 'message', msg);
}
var obj = flag(this, 'object');
new Assertion(obj, msg).is.a('function');
try {
obj();
} catch (err) {
caughtError = err;
}
if (!caughtError) {
this.assert(
caughtError,
'expected #{this} to throw an error but did not',
'expected #{this} to not throw an error but threw #{act}',
errorLike,
caughtError
);
return this;
}
var expectedConstructorName = checkError.getConstructorName(errorLike);
var actualConstructorName = checkError.getConstructorName(caughtError);
var expectedMessage = checkError.getMessage(errorLike);
var actualMessage = checkError.getMessage(caughtError);
this.assert(
checkError.sameInstance(caughtError, errorLike),
'expected #{this} to throw #{exp} but #{act} was thrown',
'expected #{this} to not throw #{exp}',
expectedConstructorName,
actualConstructorName
);
this.assert(
checkError.sameConstructor(caughtError, errorLike),
'expected #{this} to throw #{exp} but #{act} was thrown',
'expected #{this} to not throw #{exp}',
expectedConstructorName,
actualConstructorName
);
this.assert(
checkError.sameMessage(caughtError, errorLike),
'expected #{this} to throw error matching #{exp} but got #{act}',
'expected #{this} to not throw #{exp}',
expectedMessage,
actualMessage
);
flag(this, 'object', err);
} This keeps the
I understand this is all quite a change from your original PR - I'd love to hear your thoughts on this, and it certainly isn't meant to be fully prescriptive; just my interpretation on how I'd implement it. |
Hey @vikki. What do you think about the feedback I gave? Are you okay to make the changes requested? |
Hey @keithamus, Sorry I missed your responses in the end. So I originally wrote the PR to deviate as little as possible from the original code, and also keeping to what I understood by the original comments in chaijs/chai-as-promised#47 I like that your implementation of assertThrows is certainly clearer than what I ended up with, but it seems a shame that so much logic has to remain outside of the helper (checkError). I was keen to avoid changing the code that was already there too much but if you'd be open to a more substantial change, in order to make it more readable, then I'll definitely have a go at that :) Will update the branch asap |
@vikki I'm pretty confident in our test suite - so substantial changes are welcome. I'd love to see the ideas you have to get the logic down inside assertThrows. The more logic out of big assertions like this, the better 😄 |
Hi everyone, as I was moving this I got confused about the description of the According to @keithamus this should be its behaviour:
But when should it return true and when should it return false? By reading the name of this method I expect it to strictly compare two objects if they're both instances of After taking a look at how function compatibleInstance(thrown, errorLike) {
if (errorLike instanceof Error) {
return thrown === errorLike;
} else {
return true;
}
} Am I doing it wrong? If the answer for the previous question is "yes", what do you guys think should be Thanks in advance 😄 |
@lucasfcosta I think my intent with |
Hi @keithamus , However, I agree with you that's not a feature which is often used, so dropping it won't be bad too, but I just think that if we can keep it, there's no downside to it. What do you think it's better? Both options seem viable. |
Refactoring as per chaijs/chai-as-promised#47 to add a checkError utility. The purpose of this is to make it easier for plug ins to use the same logic as chai when verifying errors.
A couple of things I wasn't sure about :
Anyway happy to fix up stuff if this is helpful. Thanks!