Mock a request with a dynamic query parameter #2059
-
I'm looking at a way to intercept a request that the underlying platform provides which looks like
Current Nock implementation is
Trying with
just throws
Any suggestions greatly appeciated |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Hi, @kr8n3r. That's a great question. As the warning suggests, you shouldn't put search parameters in the request handler URLs. Those will have no effect. Instead, to match the requests you want, provide the path to the resource and access the value of the http.get('/v2/user_provided_service_instances', ({ request }) => {
const url = new URL(request.url)
const q = url.searchParams.get('q')
// Return a mocked JSON response if the "q"
// search parameter equals to a specific value.
if (q === 'space_guid:ABC-123') {
return HttpResponse.json({ mocked: true })
}
})
|
Beta Was this translation helpful? Give feedback.
-
great thanks. I assume it's similar syntax for multiple search params like |
Beta Was this translation helpful? Give feedback.
-
while above works for most requests, some mocking some api endpoints fails example for
where getBillableEvents uses Axios and has
This is where MSW fails
but mocking with query strings
the test passes, but MSW shows the warning for Q strings |
Beta Was this translation helpful? Give feedback.
-
Hey @kettanaito Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Hi, @kr8n3r. That's a great question.
As the warning suggests, you shouldn't put search parameters in the request handler URLs. Those will have no effect.
Instead, to match the requests you want, provide the path to the resource and access the value of the
q
search parameter in the response resolver. Then, decide how to handle the request based on that parameter's value.