-
-
Notifications
You must be signed in to change notification settings - Fork 512
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
Cannot clear cookies between Jest tests #1586
Comments
Hi! Thanks for reporting this. Although MSW sets the document cookie, it does that per your intention. This means that it's also your responsibility to clear that cookie between tests not to introduce a shared state that results in unreliable test runs. I can see you're already doing that right here but note that working with Lines 40 to 46 in 84282d0
I recommend you give this utility a try. Alternatively, you can research how to correctly clear |
Hi @kettanaito, Thank you for your answer. I had tried this |
Sorry to hear it didn't help. I recommend raising this in the Jest community (or Reactiflux discord) because it doesn't look like an MSW issue. I think you can even rule out MSW from this reproduction by assigning |
@kettanaito I did some more investigation and this does look like an MSW issue (or lack of documentation) to me. Here are my findings:
So what happens in the reproduction setup is that:
The solution is to clear both import { store as cookieStore } from "@mswjs/cookies";
function clearCookies() {
document.cookie.split(";").forEach((cookie) => {
document.cookie = cookie
.replace(/^ +/, "")
.replace(/=.*/, `=;expires=${new Date(0).toUTCString()}`);
});
cookieStore.clear();
window.localStorage.clear();
}
beforeEach(() => {
clearCookies();
}); In my opinion it would be nice to make that clear in the documentation, as currently MSW's internal cookie store is not mentioned anywhere. Side note: in the test setup, it seems that to prevent the cookie from being accessible through |
@antoinetissier adding an update for anyone that finds their way here from google like myself: You may have to clear your cookie without adding the function clearCookies() {
document.cookie.split(';').forEach((cookie) => {
document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;`);
});
cookieStore.clear();
window.localStorage.clear();
} |
Hey @michaelnwani, thanks for your answer. The snippet that I posted above worked for me. Maybe this has something to do with @kettanaito sorry to insist, but would you consider documenting this somewhere ? I'm happy to do a PR if you agree with the idea. |
Hello,
I am trying to clear cookies between Jest tests but I haven't been able to, even though #708 suggests it should be feasible.
I have a minimal reproduction setup as described below.
Please let me know if I missed something !
Prerequisites
Environment check
msw
versionNode.js version
18.12.1
Reproduction repository
https://github.com/antoinetissier/msw-jest-cookies
Reproduction steps
Run
yarn test
: the second test fails (the authentication is unexpectedly successful as the cookie should have been cleared).If the second test is run in isolation through
it.only()
, the test passes.Current behavior
It doesn't seem possible to clear cookies between different tests.
Expected behavior
I expect to be able to clear cookies between tests.
#708 suggests that it's possible but I haven't been able to do it.
Note: I am using
jsdom
as testing environment for Jest.The text was updated successfully, but these errors were encountered: