-
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
Support use of t.end as callback. #181
Conversation
@@ -134,7 +138,11 @@ Test.prototype.run = function () { | |||
}.bind(this)); | |||
}; | |||
|
|||
Test.prototype.end = function () { | |||
Test.prototype.end = function (err) { | |||
if (arguments.length >= 1 && Boolean(err)) { |
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.
if (arguments.length >= 1 && err) {
?
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.
why not just if (err)
?
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.
oh yeah wtf am I doing
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.
Originally this was !!err
but linter told me to Boolean(err)
so I followed blindly.
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.
Updated.
Looks good to me. @vdemedes @jamestalmage ? |
Test.prototype.end = function () { | ||
Test.prototype.end = function (err) { | ||
if (arguments.length >= 1 && Boolean(err)) { | ||
this.ifError(err); |
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.
If they've used t.plan()
, won't this increase their assertion count causing a plan error
message?
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.
What happens if you have a plan and call t.end
?
Except suggestions in comments, everything is good. |
* Binds all Test.prototype methods to current test. * Checks for truthy error as first argument to Test.prototype.end. Closes avajs#180.
0efd0de
to
baa2320
Compare
@@ -134,7 +138,11 @@ Test.prototype.run = function () { | |||
}.bind(this)); | |||
}; | |||
|
|||
Test.prototype.end = function () { | |||
Test.prototype.end = function (err) { | |||
if (arguments.length >= 1 && err) { |
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.
I'm still genuinely curious why you are checking argument length.
If arguments.length === 0
, isn't err
going to be falsy no matter what?
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.
Idea is to only log the assertion if there's some arguments passed. Alternative is have every test log an additional ifError assertion at the end.
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.
No, I'm saying
if (err) {
this.ifError(err)
}
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.
You're right.
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.
Perhaps it should always log the assertion if there's arguments.length
? i.e. intent is to use as errback handler.
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.
Hmmm.
We need it to behave consistently whether or not it's being used as a callback
t.plan(1);
t.ok(true);
t.end();
should perform the same as
t.plan(1)
t.ok(true);
setImmediate(t.end);
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.
in neither of those situations will the t.end be called with any arguments though.
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.
The use-case I'm thinking about goes something like this:
test(t => {
fs.readFile('data.txt', t.end);
});
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.
that is fine, but for your plan test do something that is not async:
test(t => {
t.plan(1);
t.ok(true);
t.end(new Error('something bad happened'));
});
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.
To avoid conflicts with t.plan()
, this.ifError(err)
could be replaced with:
this.assertError = new assert.AssertionError({
actual: err,
message: 'Callback called with an error → ' + err,
operator: 'callback'
});
this.exit();
This is the same stuff we do when promise rejects.
Also note we're opting for checking & printing error before checking if |
I don't know, but we probably should have a test for it. |
@timoxley looking at the code again I'm pretty sure you are going to get a bad error message if you use plan and end with this. Please write a test that does this: test(t => {
t.plan(1);
t.ok(1);
t.end(new Error('you should get this error message, not a plan count error'));
}); And check the message of the thrown exception. |
@timoxley I think it's down to that one |
Hey @timoxley, could you please fix this suggestion (#181 (diff)), so that we can merge it in? Thanks! |
Closes #180.