timkindberg / jest-when

Jest support for mock argument-matched return values.
MIT License
734 stars 38 forks source link

Support for function matchers breaks backward compatibility? #63

Closed ocean90 closed 3 years ago

ocean90 commented 3 years ago

Hey @timkindberg, I noticed that v3.2.0 seems to break existing tests and I'm not sure if that's expected. Based on semver I'd expect not.

You can see an example in these check results for https://github.com/tjenkinson/gh-action-auto-merge-dependency-updates/pull/113. It may take a while until the results are loaded so I'm also pasting an example here:

run › when the event name is pull_request › with an allowed actor › errors if the content type is incorrect

    expect(received).rejects.toHaveProperty(path, value)

    Expected path: "message"

    - Expected value  -  1
    + Received value  + 11

    - Unexpected repo content response
    + octokit/plugin-throttling error:
    +         You must pass the onAbuseLimit and onRateLimit error handlers.
    +         See https://github.com/octokit/rest.js#throttling
    +
    +         const octokit = new Octokit({
    +           throttle: {
    +             onAbuseLimit: (retryAfter, options) => {/* ... */},
    +             onRateLimit: (retryAfter, options) => {/* ... */}
    +           }
    +         })
    +

      377 |               Promise.resolve({ data: { type: 'unknown' } })
      378 |             );
    > 379 |             await expect(run()).rejects.toHaveProperty(
          |                                         ^
      380 |               'message',
      381 |               'Unexpected repo content response'
      382 |             );

      at Object.args [as toHaveProperty] (../node_modules/expect/build/index.js:241:20)
      at run.test.ts:379:41
      at step (run.test.ts:52:23)
      at Object.next (run.test.ts:33:53)
      at run.test.ts:27:71
      at Object.<anonymous>.__awaiter (run.test.ts:23:12)
      at Object.<anonymous> (run.test.ts:375:57)

The related test code is https://github.com/tjenkinson/gh-action-auto-merge-dependency-updates/blob/56c26aed95cf78be3712376cb2d61b4eb041cfd3/src/run.test.ts#L347-L355.

When reverting https://github.com/timkindberg/jest-when/commit/6190463734b92d43dcc974570292ac831c1d1a6c the tests are passing again. Do you have an idea what the issue might be? Thanks!

timkindberg commented 3 years ago

Ah damn... you are right. This was a dumb oversight. Of course ppl would be passing functions, for some reason I didn't see that it would conflict with just a regular function matcher. Gonna have to figure out what I want to do here.

Thank you!

timkindberg commented 3 years ago

Ok I think I came up with a decent solution. So now your regular function args should work again (sorry!). And now for the new feature I added you have to wrap the functions with when to make them behave the way I originally intended, where they become a predicate matcher.

Please update to v3.2.1 for the bug fix.

I updated the release notes and the README. https://github.com/timkindberg/jest-when/releases

ocean90 commented 3 years ago

Thank you @timkindberg!