-
-
Notifications
You must be signed in to change notification settings - Fork 535
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
Jest/Testing-Library with RTK-query, msw fails to reset handlers when testing for error case after success case. #1377
Comments
Hey, @ymoon715. Did you try running your test with the jest --runInBand MSW doesn't support parallel runs (see #474), so you need to make sure you run your tests sequentially. I believe once you add this option to your test script the issue will be gone: Could you please try this out? |
Thanks for the reply @kettanaito |
Then it’s either shared state issue that introduces between-test dependency, or a bug. |
If the following tests succeeds with |
I got the same error with react query, any solution? |
For me the problem was that React Query by default retry when a request fails, so, I disabled that in tests, @ymoon715, probably RTK works in the same way. |
I was running into the same issue when fetching data using SWR. My issue was I was fetching data with the same key, so the first request's response is cached, and was returned to me in subsequent test runs. I was able to resolve my issue by providing a clean SWR provider at every render like this:
From what I can see in the RTK docs, looks like RTK does something similar and caches response data based on some key. You might be able to solve your problem if you figure out how to invalidate cache and re-fetch for each test case even if you're using the same key. |
Tried removing/resetting cache afterEach. This did not work. |
@ymoon715 I think you need to add this to your RTK Query API
So that data is not cached when using tests |
If cache is the problem use this solution from another issue beforeEach(() => {
store.dispatch(pokemonApi.util.resetApiState());
}); Same solution but on stackoverflow from rtk query contributor: |
I'm fairly certain this has been fixed. Please upgrade your dependencies (including jest and related ones) and let me know. If it hasn't, submit a new issue with a new reproduction repo. If you are using Jest and JSDOM, use https://github.com/mswjs/jest-fixed-jsdom to make sure you don't have the broken environment to run MSW 2.0. |
Prerequisites
Environment check
msw
versionNode.js version
v16.13.1
Reproduction repository
https://github.com/ymoon715/rtk-query-msw
Reproduction steps
clone the repo at https://github.com/ymoon715/rtk-query-msw
Current behavior
I have my tests to reset the handlers after every test.
I ran into an error when testing for both success and error cases.
If i test error case FIRST, then success SECOND, it all passes.
However, if i test success case FIRST, then error case SECOND, error case fails.
I am using server.use() to test both success and error cases.
I have 4 different test files to showcase that testing error case first THEN the success case always works, where as testing success case first THEN the error case always fails
Expected behavior
server.resetHandlers() to successfully reset the handlers, or for us to be able to test for error case after successful cases.
The text was updated successfully, but these errors were encountered: