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
feat: add pending interceptor check functions to mock agent #1358
Conversation
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.
Good work!!!
Thanks for the review! I'm aware of some other nits and I'll address the review comments soon :) |
Also CI is failing |
Yep, I think CI fails because I drop ansi colouring in the output if process.env.CI is set. Gonna take a look! (Is reading that env var okay, or should I add a separate one? As an unfortunate Google Cloud Build user, I really appreciate it when tools default to no colours on CI.) |
Codecov Report
@@ Coverage Diff @@
## main #1358 +/- ##
==========================================
+ Coverage 94.25% 94.29% +0.03%
==========================================
Files 45 47 +2
Lines 4211 4240 +29
==========================================
+ Hits 3969 3998 +29
Misses 242 242
Continue to review full report at Codecov.
|
I think it should be good now, but the linter will trip until #1361 lands I think |
Can you rebase? The linter landed |
I merged main into my branch :) everything passed locally, so hopefully it all works now |
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.
lgtm
@SimenB could you take a look?
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.
This nicely covers the use case I had in mind when opening the issue. Thanks! 👍
@@ -0,0 +1,138 @@ | |||
'use strict' |
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.
there are no tests for persist
, can you add?
Also, I think a persisted interceptor should error if it's never called (I don't know if the logic does that today)
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.
there are no tests for persist, can you add?
Yep!
Also, I think a persisted interceptor should error if it's never called (I don't know if the logic does that today)
I don't think that's currently possible with the way Undici counts requests, since the times
value is only set if times(<number>)
has been called. In this PR I "cheat" by treating times
as 1
if neither persist
nor times
is set.
It could definitely be solved by tracking the call count counting upwards to either a limit
set by times()
, 1
if neither persist
nor times
is set, or forever if persist
is set, before it stops serving the mocked response.
I can give it a try, but that feels like it might belong in a separate pull request. On the other hand, it would trip on a new assertion and thus be a breaking change to the API introduced in this PR, so maybe it belongs here…
Just so I don't waste time on code that will be rejected: @mcollina or @ronag are you open to me changing the call count tracking in this PR so it can all land at once?
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.
Go for it!
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.
Done 😅
59d4abd
to
11ff74f
Compare
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
11ff74f
to
6af598d
Compare
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.
nice!
Okay, @SimenB and I sat down together, and greatly simplified the changeset. Feature-wise, the reporter now:
We also switched from calling interceptors "(un)consumed", and instead use "pending". This came with more tests and updated docs. It'd be great if you took another look 😄 Edit: Oh, and we renamed the |
@@ -107,9 +107,9 @@ function getMockDispatch (mockDispatches, key) { | |||
} | |||
|
|||
function addMockDispatch (mockDispatches, key, data) { | |||
const baseData = { times: null, persist: false, consumed: false } | |||
const baseData = { timesInvoked: 0, times: 1, persist: false, consumed: false } |
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.
we default times
to 1
here so we don't have to do null checking everywhere
const replyData = typeof data === 'function' ? { callback: data } : { ...data } | ||
const newMockDispatch = { ...baseData, ...key, data: { error: null, ...replyData } } | ||
const newMockDispatch = { ...baseData, ...key, pending: true, data: { error: null, ...replyData } } |
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.
All interceptors are pending
as they are first registered. I think consumed
belongs here as well (and not in baseData
), but… meh. I suppose it might be possible to register an interceptor that should not be pending
by registering it with .times(0)
, but that sounds too illogical to account for.
@@ -230,25 +230,20 @@ function mockDispatch (opts, handler) { | |||
const key = buildKey(opts) | |||
const mockDispatch = getMockDispatch(this[kDispatches], key) | |||
|
|||
mockDispatch.timesInvoked++ |
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.
We now increase timesInvoked
instead of decreasing times
(which is now constant).
(Is invoked
a good name, or would something like timesIntercepted
be better?)
} | ||
// If it's used up and not persistent, mark as consumed | ||
mockDispatch.consumed = !persist && timesInvoked >= times | ||
mockDispatch.pending = timesInvoked < times |
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.
consumed
is the same as before, but pending
is new.
Persistent: persist ? '✅' : '❌', | ||
Invocations: timesInvoked, | ||
Remaining: persist ? Infinity : times - timesInvoked | ||
})) |
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.
we moved the header name mapping here so that the formatter gets proper lowerCamelCased variable names passed in. I think that makes it less annoying to build a new formatter. Otherwise, this does the same thing as before
@@ -0,0 +1,138 @@ | |||
'use strict' |
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.
Done 😅
return | ||
} | ||
|
||
const pluralizer = new Pluralizer('interceptor', 'interceptors').pluralize(pending.length) |
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 pluralizer
API ends up being a little more complicated than it maybe needs to now that we only track one array (instead of consumed
, tooFewUses
, and persistent
as we did before), but I think it's still a nice little utility.
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.
lgtm
Awesome! Is there anything else I can do to help this land? 😄 |
Yay! |
Testing this out, works great 👍 not sure if Only thing I can think of that might make this even better is to capture an |
Hi! 👋
This adds functions to check any pending interceptors on a mock agent, as well as a function that throws with a friendly, emoji-ful error message if there are any pending interceptors.
I think this closes #1002
I've never contributed here before, so please let me know if I should change anything! 🙌
(Huge thanks to @SimenB for helping me port it from our own repo)