Skip to content

feat: do not exhaust one-time handlers with custom predicate in resolver #2472

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/handlers/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ export abstract class RequestHandler<
if (!this.resolverIterator) {
const result = await resolver(info)
if (!isIterable(result)) {
this.isUsed = result instanceof Response
Copy link
Contributor Author

@ytoshiki ytoshiki Apr 11, 2025

Choose a reason for hiding this comment

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

I wasn’t able to apply the change in the run method as you suggested, because we need to take generator use cases into consideration.

Also, I kept this line as is to ensure parallel processing works correctly.

this.isUsed = true

Please let me know if you have any suggestions or thoughts on this.

return result
}
this.resolverIterator =
Expand All @@ -346,8 +347,7 @@ export abstract class RequestHandler<
if (done) {
// A one-time generator resolver stops affecting the network
// only after it's been completely exhausted.
this.isUsed = true

this.isUsed = nextResponse instanceof Response
// Clone the previously stored response so it can be read
// when receiving it repeatedly from the "done" generator.
return this.resolverIteratorResult?.clone()
Expand Down
31 changes: 31 additions & 0 deletions test/node/msw-api/setup-server/use.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,34 @@ test('throws if provided the invalid handlers array', async () => {
'[MSW] Failed to call "use()" with the given request handlers: invalid input. Did you forget to spread the array of request handlers?',
)
})

test('does not consume "once" handler if it does not return a response', async () => {
let shouldHandle = false

server.use(
http.get(
httpServer.http.url('/book/:bookId'),
({ request }) => {
if (!shouldHandle) {
return undefined
}
return HttpResponse.json({ title: 'One-time override' })
},
{ once: true },
),
)

const res1 = await fetch(httpServer.http.url('/book/abc-123'))
expect(res1.status).toBe(200)
expect(await res1.json()).toEqual({ title: 'Original title' })

shouldHandle = true

const res2 = await fetch(httpServer.http.url('/book/abc-123'))
expect(res2.status).toBe(200)
expect(await res2.json()).toEqual({ title: 'One-time override' })

const res3 = await fetch(httpServer.http.url('/book/abc-123'))
expect(res3.status).toBe(200)
expect(await res3.json()).toEqual({ title: 'Original title' })
})