Skip to content
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

test: added common.mustCallAtLeast #12935

Merged
merged 0 commits into from
May 19, 2017
Merged

test: added common.mustCallAtLeast #12935

merged 0 commits into from
May 19, 2017

Conversation

refack
Copy link
Contributor

@refack refack commented May 9, 2017

added common.mustCallAtLeast for calling more than minimum times.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

test

@nodejs-github-bot nodejs-github-bot added the test Issues and PRs related to the tests. label May 9, 2017
@refack refack self-assigned this May 9, 2017
@refack refack requested review from jasnell and addaleax May 9, 2017 21:25
/**
* @function mustCall
* @param fn "The code that must be called
* @param {?number|string} expected "Expected times to be called, or '+' for more then 0. Default = 1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: then → than

@refack
Copy link
Contributor Author

refack commented May 9, 2017

CI: https://ci.nodejs.org/job/node-test-commit/9765/
/cc @nodejs/testing

* @returns {Function}
* @export
*/
exports.mustCall = function mustCall(fn, expected) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you undo this block of changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the JSDocs?
It's usefull for people with IDEs that can parse these (VSCode / Webstorm)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than lib/punycode.js which is vendored, there are only 9 @param matches in all of lib, src, and test. It's more consistent to drop them.

@@ -0,0 +1,16 @@
'use strict';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should live in test/parallel/test-common.js I think.

Copy link
Contributor Author

@refack refack May 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't see that one...

@cjihrig
Copy link
Contributor

cjihrig commented May 9, 2017

I think this needs a documentation update.

@refack refack mentioned this pull request May 9, 2017
3 tasks
@refack
Copy link
Contributor Author

refack commented May 9, 2017

Docs added, nit's addressed. @cjihrig I'd rather keep the JSDocs, unless you have a strong objection.

@cjihrig
Copy link
Contributor

cjihrig commented May 9, 2017

I wouldn't say strong objection, but definitely -1.

@refack
Copy link
Contributor Author

refack commented May 9, 2017

I wouldn't say strong objection, but definitely -1.

Gone.
I'll do a PR with JSDocs for the whole file once 😉

@Fishrock123
Copy link
Contributor

Could you point to somewhere that this would be better suited for rather than the explicit number of calls?

@Fishrock123
Copy link
Contributor

I generally think this is a bad idea because if something is being called more times than you expect you've probably got a bug.

@refack
Copy link
Contributor Author

refack commented May 9, 2017

Could you point to somewhere that this would be better suited for rather than the explicit number of calls?

#12930 (comment)

@refack
Copy link
Contributor Author

refack commented May 9, 2017

I generally think this is a bad idea because if something is being called more times than you expect you've probably got a bug.

I tend to agree, but some things are either OS dependent or non deterministic. In those cases we tend to skip the mustCall all together which IMHO is worse.
I agree this should be used sparingly, and usage should receive justification in a comment.

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been wanting this for a while. Once @cjihrig is happy with it, I'm LGTM

@mscdex
Copy link
Contributor

mscdex commented May 9, 2017

-1 I don't think it is a good idea to reuse the same parameter (or even the same function) for this.

@jasnell
Copy link
Member

jasnell commented May 9, 2017

@Fishrock123 ... this came up with regards to adding mustCall() around a data event handler, which could be reasonably called any number of times.

@mscdex ... the other option I had in mind was a common.mustCallAtLeast(n) method that would allow a minimum number to be specified.

@mscdex
Copy link
Contributor

mscdex commented May 9, 2017

@jasnell I would much prefer a separate method so that the intention is clear.

@refack
Copy link
Contributor Author

refack commented May 9, 2017

@jasnell I would much prefer a separate method so that the intention is clear.

@mscdex I was thinking about that.
Will do.

Copy link
Member

@Trott Trott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-0 from me. I dislike magic values. I prefer that things in common have simple, self-explanatory, and intuitively obvious interfaces. This adds more cognitive overhead, especially for newcomers.

fail.
Returns a function that calls `fn`. If the returned function has not been
called exactly `expected` number of times, or at least once if
`expected === '+'`, when the test is complete, then the test will fail.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This documentation change results in a confusing run-on sentence:

If the returned function has not been called exactly expected number of times, or at least once if
expected === '+', when the test is complete, then the test will fail.

Would be better as something like:

If the returned function has not been called exactly expected number of times when the test is complete, then the test will fail. If expected is '+'`, then the test must run at least once.

@gibfahn
Copy link
Member

gibfahn commented May 9, 2017

the other option I had in mind was a common.mustCallAtLeast(n)

+1 to that

-0 from me. I dislike magic values

@Trott thoughts on @jasnell's proposed common.mustCallAtLeast(2)?

@Trott
Copy link
Member

Trott commented May 9, 2017

@Trott thoughts on @jasnell's proposed common.mustCallAtLeast(2)?

Eh, still pretty +/-0 on it. I think there's enough API surface area in common and we quickly reach a point of diminishing returns.

Moreover, I'm not really sure this is solving a very common problem. I imagine it's mostly for data callbacks in tests that are called once but could theoretically be called more than once. However, those almost never need to actually be wrapped to make sure they're called because they usually have something like data += chunk and then the value of data is checked somewhere, so you don't really need to check that the callback was called. The test will fail if it wasn't. (Will there be a small number of empty callbacks for data that we want to check? Sure. Is it worth adding a whole other function to the common monolith for it? I doubt it.)

But if others feel this has big value, I won't stop it.

@refack
Copy link
Contributor Author

refack commented May 9, 2017

Replace magic '+' with mustCallAtLeast PTAL

@refack refack changed the title test: allow mustCall '+' as expected test: added common.mustCallAtLeast May 9, 2017
@refack
Copy link
Contributor Author

refack commented May 9, 2017

P.S. why don't we adopt something like sinon.
I'm a big believer that better tooling begets better tests.

@Trott
Copy link
Member

Trott commented May 10, 2017

P.S. why don't we adopt something like sinon.

Biggest reason is probably because no one's bothered to do it yet.

That said, a PR adding sinon very well might not get accepted. Speaking only for myself, I have grown wary of things that raise the barrier to entry for people working on tests. So to the extent that we avoid extra tools and unneeded abstractions in the common module, I'm happy. But that said, if the benefit would be considerable and obvious, I'd be for it.

Others in @nodejs/testing might have other opinions.

@refack
Copy link
Contributor Author

refack commented May 19, 2017

@refack
Copy link
Contributor Author

refack commented May 19, 2017

landed in fccc0bf

@refack refack deleted the mustCall+ branch May 19, 2017 19:24
@refack refack merged commit fccc0bf into nodejs:master May 19, 2017
@jasnell jasnell mentioned this pull request May 28, 2017
@refack refack removed their assignment Jun 12, 2017
@gibfahn gibfahn mentioned this pull request Jun 15, 2017
3 tasks
@MylesBorins
Copy link
Contributor

Should this land on v6.x?

@refack
Copy link
Contributor Author

refack commented Jul 17, 2017

Should this land on v6.x?

Sure. It's a semver-minor change in the test harness.

@MylesBorins MylesBorins added the semver-minor PRs that contain new features and should be released in the next minor version. label Jul 17, 2017
@refack
Copy link
Contributor Author

refack commented Jul 17, 2017

#14327 depends on this

@MylesBorins MylesBorins added land-on-v6.x and removed lts-watch-v6.x semver-minor PRs that contain new features and should be released in the next minor version. labels Aug 14, 2017
MylesBorins pushed a commit that referenced this pull request Aug 14, 2017
PR-URL: #12935
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request Aug 16, 2017
PR-URL: #12935
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@MylesBorins MylesBorins mentioned this pull request Aug 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants