Repository navigation
fix: await session and cookie writes in browser client loginAs - #270
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
setSession and setCookie are async. Without awaiting them, loginAs could resolve before the user was logged in, and a closed browser context could surface as an unhandled rejection after the test finished. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
|
Thanks for the fix 👍🏽 |
Fixes #269
What
In the Japa browser client plugin,
browserContext.loginAsandbrowserContext.withGuard(...).loginAsnow await the session and cookie writes before they resolve:await context.setSession(requestData.session)context.setCookie(...)call is awaited (theforEachis replaced so the promises aren't dropped)Why
Both calls are async.
setSessionreads cookies through Playwright and commits the session store.setCookiecallsbrowserContext.addCookies. Because they weren't awaited:await loginAs(user)could resolve before the user was actually logged in.setSessionpromise rejected withbrowserContext.cookies: Target page, context or browser has been closed. Japa reports that as an unhandled rejection and exits with code 1 even when every test passed.Tests
Added two tests to the
Browser client | loginAsgroup intests/auth/plugins/browser_client.spec.ts:wait for the session to be persisted before resolving loginAswait for the cookies to be persisted before resolving loginAsEach one stubs
authenticateAsClientto return a session or cookies. It replacesbrowserContext.setSessionorbrowserContext.setCookiewith an async stub that yields one tick (setImmediate). Then it checks that bothloginAsandwithGuard('web').loginAsresolve only after the stub completes.expected [ false, false ] to deeply equal [ true, true ]🤖 Generated with Claude Code
Summary by CodeRabbit