Skip to content

test: added common.mustCallAtLeast - #12935

Merged
refack merged 0 commit into
nodejs:masterfrom
refack:mustCall+
May 19, 2017
Merged

test: added common.mustCallAtLeast#12935
refack merged 0 commit into
nodejs:masterfrom
refack:mustCall+

Conversation

@refack

@refackrefack commented May 9, 2017

Copy link
Copy Markdown
Contributor

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-botnodejs-github-bot added the test Issues and PRs related to the tests. label May 9, 2017
@refackrefack self-assigned this May 9, 2017
@refack
refack requested review from addaleax and jasnellMay 9, 2017 21:25
Comment threadtest/common/index.js Outdated

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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

Comment threadtest/common/index.js Outdated

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
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.

Comment threadtest/parallel/test-test-common.js Outdated

Copy link
Copy Markdown
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.

@refackrefackMay 9, 2017

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
Contributor

I think this needs a documentation update.

@refackrefack mentioned this pull request May 9, 2017
3 tasks
@refack

Copy link
Copy Markdown
ContributorAuthor

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

@cjihrig

Copy link
Copy Markdown
Contributor

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

@refack

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
Contributor

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

@Fishrock123

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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

#12930 (comment)

@refack

Copy link
Copy Markdown
ContributorAuthor

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.

@jasnelljasnell left a comment

Copy link
Copy Markdown
Member

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

mscdex commented May 9, 2017

Copy link
Copy Markdown
Contributor

-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
Copy Markdown
Member

@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
Copy Markdown
Contributor

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

@refack

Copy link
Copy Markdown
ContributorAuthor

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

@mscdex I was thinking about that.
Will do.

@TrottTrott left a comment

Copy link
Copy Markdown
Member

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.

Comment threadtest/common/README.md Outdated

Copy link
Copy Markdown
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
Copy Markdown
Member

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

Trott commented May 9, 2017

Copy link
Copy Markdown
Member

@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
Copy Markdown
ContributorAuthor

Replace magic '+' with mustCallAtLeast PTAL

@refackrefack changed the title test: allow mustCall '+' as expectedtest: added common.mustCallAtLeastMay 9, 2017
@refack

Copy link
Copy Markdown
ContributorAuthor

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

@Trott

Copy link
Copy Markdown
Member

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
Copy Markdown
ContributorAuthor

@refack

Copy link
Copy Markdown
ContributorAuthor

landed in fccc0bf

@refack
refack deleted the mustCall+ branch May 19, 2017 19:24
@refack
refack merged commit fccc0bf into nodejs:masterMay 19, 2017
@jasnelljasnell mentioned this pull request May 28, 2017
@refackrefack mentioned this pull request Jun 3, 2017
4 tasks
@refackrefack removed their assignment Jun 12, 2017
@gibfahngibfahn mentioned this pull request Jun 15, 2017
3 tasks
@MylesBorins

Copy link
Copy Markdown
Contributor

Should this land on v6.x?

@refack

Copy link
Copy Markdown
ContributorAuthor

Should this land on v6.x?

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

@MylesBorinsMylesBorins 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
Copy Markdown
ContributorAuthor

#14327 depends on this

@MylesBorinsMylesBorins 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>
@MylesBorinsMylesBorins mentioned this pull request Aug 16, 2017
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testIssues and PRs related to the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@refack@cjihrig@Fishrock123@mscdex@jasnell@gibfahn@Trott@MylesBorins@addaleax@nodejs-github-bot