RTK Query and msw
testing - create mocked endpoints easily
#4915
andyl-ioki
started this conversation in
Ideas
Replies: 1 comment 6 replies
-
how would you take |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hej there!
First of all: Thanks for maintaining and providing this amazing library. It helps a lot 👍
Currently we are migrating to RTK Query and in this steps we are also migrating the tests so that they use
msw
mocked endpoints. So far so good.What bothers me: We have lots of endpoints and lots of different cases to mock. So this results in lots of repetitive code, which is not really maintainer friendly 😅
An idea would be a function that takes any endpoint from RTK Query and a response value. It then returns a
msw
HttpHandler
, which you can integrate in your tests:const mockEndpoint = (endpoint: QueryDefinition | MutationDefinition, resolver: HttpResponseResolver): HttpHandler ...
The problem with this idea is that (as far as I got it): I can't access the RTKQ endpoints build data/parameters. Am I right or did I miss something?
Alternatively I could create objects which contain those data and pass them to RTKQ and MSW - but it looks a bit overwhelming and less maintainable than just "extracting" the data from RTK Query builders.
Example:
Beta Was this translation helpful? Give feedback.
All reactions