Expose cookieStore
#2467
Replies: 3 comments 4 replies
-
|
Hi, @michael-slx. Could you please tell me more about your use case? I feel I'm missing the context. It would be great to include an example of where you want to do something with that The idea is that the cookie store is an internal mechanism that you shouldn't reference or otherwise interface in your apps/tests. If you want to set a cookie, you don't reach for MSW internals, you just mock a response with the You can still set whatever you want on I can advise a better course of action once I hear more from you. Thanks. |
Beta Was this translation helpful? Give feedback.
-
|
Hi @kettanaito, I am developing a Next application that has to interface with a legacy API that only supports cookie-based authentication. I do not want the browser to interact with that API hence it will completely happen on the server-side (i.e. Node.js). Since Node’s global I do not want MSW to store any cookies because I'm trying to test the cookie handling of my own code. I cannot clear out Thanks for your help in advance! ExampleHandlersexport const handlers = [
// HTTP "echo" endpoint
// Sends back basic infos about the request
http.all("http://localhost/echo", ({ request, cookies }) => {
const body = {
method: request.method,
query: new URL(request.url).searchParams.entries().toArray(),
headers: request.headers.entries().toArray(),
cookies: cookies,
};
return HttpResponse.json(body);
}),
// Set cookie endpoint
// Sets a cookie named `random` to a random UUID
http.post("http://localhost/set-cookie", ({ request, cookies }) => {
const name = "random";
const value = crypto.randomUUID();
const body = { name, value };
return HttpResponse.json(body, {
headers: {
"Set-Cookie": `${name}=${value}`,
},
});
}),
];Integration testdescribe("createFetchWithCookies", () => {
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
// TEST WORKS OK
test("Cookie handling", async () => {
const { fetch } = await createFetchWithCookies();
// Sets cookie
const setCookieRes = await fetch("http://localhost/set-cookie", { method: "POST" });
const setCookieJson = await setCookieRes.json();
const { name: cookieKey, value: cookieValue } = setCookieJson;
// Retrieve request info
const res = await fetch("http://localhost/echo");
const resJson = await res.json();
expect(res.cookies[cookieKey]).toEqual(cookieValue);
});
// TEST FAILS
// Reason: MSW stores the cookie from the test above.
test("Sends no cookies by default", async () => {
const { fetch } = await createFetchWithCookies();
const res = await fetch("http://localhost/echo");
const resJson = await res.json();
expect(Object.keys(res.cookies)).toHaveLength(0);
});
}); |
Beta Was this translation helpful? Give feedback.
-
|
+1 to this. Another idea is to add an option to disable cookie storage entirely, which would match Node's fetch behavior. For my use case, I'd like to reuse the mocks I have between local dev and playwright tests via a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Scope
Adds a new behavior
Compatibility
Feature description
Background / use case
I’m working on a server-side cookie implementation (Node), which I test using Vitest + MSW. However, MSW’s cookie "emulation" interferes with my own since both are storing cookies.
Solution
I personally think the easiest solution would be to export the global
cookieStorethat MSW uses. This would allow library consumers to clear (or set) cookies if it is necessary for their use case.If desired, I'm happy to open a PR but I’m unsure whether a test is necessary and how it should be tested.
Beta Was this translation helpful? Give feedback.
All reactions