Question: request handler request history
See original GitHub issueIs your feature request related to a problem? Please describe.
When using msw
in a test environment, I’d like to be able to retrieve history of requests / responses for a certain handler. Do they get stored anywhere?
When using axios, there’s a way to do it if you use axios-mock-adapter
, that provides history per method.
Without mocking axios, I can’t find a way to retrieve the last request made so I can verify that a request with certain params had been made.
Given
export const findPets = (petResponse?: Pet[]) =>
rest.get(`${endpoint}/pets`, (req, res, ctx) => {
const PetResponse = petResponse ?? PetStub.buildList(10)
return res(ctx.delay(0), ctx.status(200), ctx.json(PetResponse))
})
PetsList.test
it('should return n results when limit is set', async () => {
const FindPets = findPets()
server.use(FindPets)
render(<PetsList />) // Component makes a request with a query param {limit: 2}
await waitFor(() => screen.getByText(/List of pets/i))
/* Here I'd like to be able to retrieve GET request and assess it's params so I could validate that component's sending proper params, a la
expect(FindPets.history[0]).toEqual(expect.objectContaining({ limit: 2})
*/
})
Additional context
Name | Version |
---|---|
msw | 0.20.5 |
node | 13 |
axios | 0.19.2 |
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (6 by maintainers)
Top Results From Across the Web
Request Handler - an overview | ScienceDirect Topics
This handler responds to the request message with a read operation to return the data value. This handler runs to completion. Note that...
Read more >How Requests are Handled | App Engine standard ...
App Engine calls the handler script with a Request and waits for the script to return; all data written to the standard output...
Read more >Invalid Request Handler - Ivanti
Purpose. The Invalid Request Handler checks requests for invalid specifications of: HTTP method; URI; Argument; Parameter; Header; Body.
Read more >How can i call the whole html page to a request handler ...
I have an html page with a simple form with a post method and a text area field. I want to call the...
Read more >Handle Requests Sent by Alexa | Alexa Skills Kit
Yes. About request handlers. A request handler is the code responsible for taking action on one or more types of incoming requests. When ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey, @ilyaulyanov. Thanks for reaching out.
I’m afraid accessing such list and performing assertions on it would be implementation details testing. Instead of checking internals of MSW and looking up if some handler was called with some parameters, think of it this way: if a handler is called with invalid parameters, your React component’s output will be invalid. Assert what your component renders, not the network layer.
Take a look at the similar question that’s been raised recently. I go into details and provide usage examples that lean your test towards testing what matters.
I can see such API being useful only for development purposes. It brings too much temptation to test if something was called, or was called with the right parameters, which is something MSW actively discourages. Test how your component reacts to the response, emulate unsuccessful and error responses, provide conditional mocked responses based on the request parameters in your response resolvers—this will give you much more confidence in your tests.
Hope this helps.
@ilyaulyanov consider reusing the condition logic from the rest of your app. Since mocks execute in the same context, you can call the same validation functions in the mocks, for example, that you do in the actual application’s implementations. This is a solid way of ensuring you don’t get false positive/negative results in your tests.
It may feel like repetition, but in reality it’s depicting the behaviors that make your request valid. Not all business logic should be copied to response resolvers. The one that does helps you to reason about API changes over time, acting like a snapshot of an API contract once established. This is one of the arguments against any kind of sync between the actual server and mocks: it would destroy the reproducibility of a mock.
Thank you for the suggestion, I think it’s a good idea to put these recommendations into a recipe. I’ve added the Request assertions recipe, please take a look.