Next.js Discord

Discord Forum

Cookies not usable for playwright tests and vice versa

Unanswered
European pilchard posted this in #help-forum
Open in Discord
European pilchardOP
I have protected routes that are protected via user session/jwt that is retrieved and stored from my login() server action. This works fine manually, but not for automated tests. Playwright cannot create the session when automating the login user flow (filling inputs, clicking login, waiting for post request response), and if I create the cookie with playwright's context().addCookies(), the cookie is created and usable by playwright, but nextjs cannot read/detect the cookie.

Any attempt to directly or indirectly call the next/headers cookies() function results in the invariant: cookies() expects to have requestasyncstorage none available error.

Although I am 99% sure this feature will work in production and already works normally in dev, my company/team requires testing and this bug makes it very difficult to test. There are no examples in either the NextJS docs or playwright docs that showcase this.

1 Reply

Are you using msw? I've found that next/headers functions do not work if you're also using msw, for some reason.