-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Return thrown error/rejected promise from t.throws #576
Conversation
By analyzing the blame information on this pull request, we identified @ariporad, @SamVerschueren and @sotojuan to be potential reviewers |
err = function (err) { | ||
return err.message === errMsg; | ||
}; | ||
} | ||
|
||
assert.throws(fn, err, msg); | ||
|
||
if (errMsg) { | ||
return Error(errMsg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
errMsg
is the expected message. The workaround above stems from #125 and was done because assert.throws
doesn't take a string argument as the expected message, but we did want that behavior in AVA.
In other words it's not something to be returned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might actually be able to kill this if-block entirely given your suggestion at https://github.com/sindresorhus/ava/pull/576/files#r54448454. I'll give it a whirl!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're returning a very different value here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's gone!
I don't understand why you're changing No need to exhaustively test |
Cool getting there! |
This might be a case of me not writing the tests as one should to get it working as expected. But when going back and reverting the changes made to // test/promise.js
test('throws becomes the promise rejection', function (t) {
var rejection;
ava(function (a) {
a.plan(1);
rejection = new Error('foo');
var promise = Promise.reject(rejection);
return a.throws(promise);
}).run().then(function (result) {
t.is(result.passed, true);
t.is(result.result.assertCount, 1);
t.is(result.reason, rejection);
t.end();
});
}); this line is the real deal-breaker in this specific case. Without it, I don't see how one would return the rejection reason to the callee. Again, it's not that far-fetched that this test does not in fact belong here and I'm just doing it wrong. On that note, my pizza just arrived. I'll be back in a bit 🍕 |
Sorry, I phased out after having my supper last night (I blame Comedy Central). I got around to cleaning some stuff out, but I'm sort of at a loss on how to handle the test case for a Promise rejection. So far I'm at this point; test('.throws() becomes the rejection reason of promise', function (t) {
var expected = new Error();
assert.throws(Promise.reject(expected)).then(function (err) {
assert.same(expected, err);
t.end();
});
}); But that's not actually testing the return value of |
Alright, I reckon we're good to go there with the latest changes made.
Let me know what you think, I've got an itch in my squash finger. I guess a brief look from @jamestalmage would be warranted as well, given he's the author of #493. |
To me, returning object with |
@vdemedes Sorry, I should've probably updated the OP. Like I stated before, I wasn't all too comfy with overriding the return value of The way it works now is this; test(t => {
const expected = new Error();
const actual = t.throws(Promise.reject(expected));
t.same(expected, actual);
}); Whereas what I was doing in the beginning was something like so; test(t => {
const expected = new Error();
return t.throws(Promise.reject(expected));
}).then(result) {
// result.reason === expected
} I was trying to cater to the I'll gladly edit the OP when I get a on the train. At work currently. |
Oh great, so it does return thrown Error from |
It does indeed! :) |
@@ -182,6 +183,29 @@ test('.throws()', function (t) { | |||
t.end(); | |||
}); | |||
|
|||
test('.throws() becomes the thrown Error', function (t) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returns
rather than becomes
. Could lowercase Error
as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you like for me to change the subject of the Promise rejection while I'm at it? @novemberborn
'.throws() becomes the rejection reason of promise'
'.throws() returns the rejection reason of promise'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, '.throws() returns the rejection reason of promise' is a correct version indeed.
@kasperlewau cool! Just some simplifications in the test. Feel free to push up a squashed commit when that's fixed and I'll merge. |
@novemberborn Resolved, squashed, pushed. Let the CI wait commence. |
throw expected; | ||
}); | ||
|
||
assert.same(actual, expected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops wait. assert.same()
means "deep equal". We want strict equality here. You should use assert.is(actual, expected)
instead. Though the tests here are for AVA's assert
module. Perhaps use t.is(actual, expected)
which is from tap
's assertion library (AVA's tests are run using tap
).
Same below.
@kasperlewau OK just the tiniest issue with the assertion. Feel free to force push again and I'll merge. |
Right-o! I'm still fairly new to Off topic; I'll get this sorted out! |
Rebasing and pushing in a jiffy. |
Return thrown error/rejected promise from t.throws
Thanks @kasperlewau! |
Glad I could help! : ) |
Thank you! Keep'em coming :) |
Thanks a lot! |
PR #576 changed the `t.throws()` assertion to return the thrown error, or if asynchronous a promise for the rejection reason. Unfortunately this only worked for asynchronous errors. The tests cover the changes in `lib/assert.js` but `t.throws()` is an *enhanced* assertion. This commit ensures any values returned from `lib/assert.js` assertions are indeed returned by the corresponding `t.` assertions.
If a
throws
assertion passes, we should return the reason for said pass. That entails;Loosely based on the description of #493. I consider it to be loosely based on it, because of the following differences (in the Promised-land):
I hijacked the
result.reason
property because I wasn't fully comfortable in excluding the baseresult
properties out of a successfulthrows
assertion, which was heavily used intest/promise
andtest/observable
. However this can be changed quite easily, to more closely reflect the initial desires of #493.Haven't taken a stab at docs for the changes made yet, I'll get on top of that on my way home from work.