-
-
Notifications
You must be signed in to change notification settings - Fork 518
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
All once
handlers are being flagged as used as soon as a request is intercepted, no matter the URL
#1782
Comments
It seems you're using lower-level primitives to test the behavior, whereas in my reproduction I'm using plain Or maybe you're using a different Node version? I'm having the problem on I'm also using Vitest, not Jest, both in my own repo and in the reproduction one. I don't have any idea whether that could influence the results. |
I ran into this today. It seems that the problem is here: msw/src/core/handlers/RequestHandler.ts Lines 193 to 210 in 88c45ea
It immediately marks the handler as used, to prevent two back-to-back requests from both going through. However, parsing the request, to determine if the handler should be invoked, is asynchronous (as needed by the GraphQL handler). There appears to be no way to have both "immediately enforce 'once'" and "asynchronously determine whether it's used", unless you do something such as changing Here's a test in (collapsed for a shorter issue description)it('does not mark non-matching one-time handlers as used', async () => {
const { emitter } = setup()
const oneTimeHandler = http.get(
'/resource',
() => {
return HttpResponse.text('One-time')
},
{ once: true },
)
const anotherHandler = http.get(
'/another',
() => {
return HttpResponse.text('Another')
},
{ once: true },
)
const handlers: Array<RequestHandler> = [oneTimeHandler, anotherHandler]
const requestId = uuidv4()
const firstResult = await handleRequest(
new Request('http://localhost/another'),
requestId,
handlers,
options,
emitter,
callbacks,
)
expect(await firstResult?.text()).toBe('Another')
expect(oneTimeHandler.isUsed).toBe(false)
expect(anotherHandler.isUsed).toBe(true)
const secondResult = await handleRequest(
new Request('http://localhost/resource'),
requestId,
handlers,
options,
emitter,
callbacks,
)
expect(await secondResult?.text()).toBe('One-time')
expect(anotherHandler.isUsed).toBe(true)
expect(oneTimeHandler.isUsed).toBe(true)
}) |
Released: v2.0.2 🎉This has been released in v2.0.2! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Prerequisites
Environment check
msw
versionNode.js version
v18.18.0
Reproduction repository
https://stackblitz.com/edit/vitest-dev-vitest-xa3evs?file=test%2Fbasic.test.ts
Reproduction steps
Just let the tests run (which should happen as soon as you land on the page), the green test highlights that it shouldn't behave that way.
Current behavior
I've got a detailed answer here already, but for good measure:
once
handler, for instance forhttps://test.local/foo/trpc/getById
https://test.local/bar/trpc/getByName
https://test.local/foo/trpc/getById
), see that it's not properly handled and that theonce
handler had already been flagged as usedI've seen this behavior in
0.0.0-fetch.rc-23
and0.0.0-fetch.rc-24
.Expected behavior
The
once
handler registered forhttps://test.local/foo/trpc/getById
should not have been flagged as used after the first request, because it didn't match. In the reproduction repo, there should even still be oneonce
unused handler in the pool, since only one request tohttps://test.local/foo/trpc/getById
has been triggered, whereas there were twoonce
handlers registered for that URL.The text was updated successfully, but these errors were encountered: