testdouble.js icon indicating copy to clipboard operation
testdouble.js copied to clipboard

export argsMatch

Open lytc opened this issue 7 years ago • 3 comments

Description

It's useful to test what arguments it was nth called with. In my case is when I implement Jest matcher (toHaveBeenLastCalledWith, toHaveBeenNthCalledWith ) for testdouble.

Example Repo

https://github.com/lytc/jest-testdouble/blob/master/src/index.js#L121

lytc avatar Sep 10 '18 18:09 lytc

Ah, this is an interesting proposal.

Our argsMatch implementation is so complex that I think exporting it for tooling reasons makes sense.

Questions:

  • Should we yank it out into a third party dependency and then have tdjs depend on it?
  • If we just add it to the top-level td, what should we name it? Should it even sit on the top level, or is that too confusing, since people shouldn't normally need it?

searls avatar Sep 14 '18 21:09 searls

@searls How about if we extends td.explain to support expectedArgs? Something like this:

const increment = td.func();
increment(1);
increment(2);
const explanation = td.explain(increment(2)); /*
{
  ...
  calls: [
    { args: [1], context: window, match: false }
    { args: [2], context: window, match: true }
  ]
}
*/

And also we can reuse td.explain for td.verify!

lytc avatar Sep 16 '18 04:09 lytc

That is a very cute API proposal, but if you really dig into the implementation of how td.verify and td.when detect those invocations, you would probably agree it is not a good idea to add to a third use case!

I've put some thought into this and I think it's probably best to just slam it on the top-level td export, since we've already broken the pudding skin and have a bunch of other random junk sitting on that level. I would prefer a name that was more intent revealing than argsMatch however, because I'd like to be able to document this to mean "function you call to verify whether test double is going to think a set of args is going to match a set of matchers"

searls avatar Sep 21 '18 12:09 searls

Stale. Closing. Please reopen if still relevant and I will look into it.

giltayar avatar Sep 16 '23 11:09 giltayar