Skip to content

fix: await session and cookie writes in browser client loginAs - #270

Merged
thetutlage merged 2 commits into
adonisjs:10.xfrom
tp-abtion:fix/await-browser-client-session
Oct 16, 2026
Merged

thetutlage merged 2 commits into
adonisjs:10.xfrom
tp-abtion:fix/await-browser-client-session

Conversation

@tp-abtion

@tp-abtion tp-abtion commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #269

What

In the Japa browser client plugin, browserContext.loginAs and browserContext.withGuard(...).loginAs now await the session and cookie writes before they resolve:

  • await context.setSession(requestData.session)
  • each context.setCookie(...) call is awaited (the forEach is replaced so the promises aren't dropped)

Why

Both calls are async. setSession reads cookies through Playwright and commits the session store. setCookie calls browserContext.addCookies. Because they weren't awaited:

  • await loginAs(user) could resolve before the user was actually logged in.
  • If the test finished and the browser context closed first, the floating setSession promise rejected with browserContext.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 | loginAs group in tests/auth/plugins/browser_client.spec.ts:

  • wait for the session to be persisted before resolving loginAs
  • wait for the cookies to be persisted before resolving loginAs

Each one stubs authenticateAsClient to return a session or cookies. It replaces browserContext.setSession or browserContext.setCookie with an async stub that yields one tick (setImmediate). Then it checks that both loginAs and withGuard('web').loginAs resolve only after the stub completes.

  • Without the fix: both fail with expected [ false, false ] to deeply equal [ true, true ]
  • With the fix: both pass

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Browser login now waits for cookies and session data to finish saving before completing, helping ensure authenticated requests start with fully persisted login state.
  • Tests
    • Added coverage for asynchronous cookie and session persistence in both browser login flows.

tp-abtion and others added 2 commits October 14, 2026 10:20
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>
@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 55bfcafc-83e4-4ac5-a9f4-0aa72b0f28f4
📥 Commits

Reviewing files that changed from the base of the PR and between 017ec31 and ce8df2c.

📒 Files selected for processing: 2
  • src/plugins/japa/browser_client.ts
  • tests/auth/plugins/browser_client.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

📝 Walkthrough

Both browser client loginAs paths now wait for session and cookie writes to complete. New tests check that calls using the default and explicit guard resolve only after the simulated persistence completes.

Changes: Browser client login

Layer / File(s) Summary
Await session and cookie writes
src/plugins/japa/browser_client.ts, tests/auth/plugins/browser_client.spec.ts
Both loginAs paths await setSession when session data is present and await each setCookie call. Tests verify that both paths wait for simulated session and cookie writes.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to ce8df

No actionable merge-blocking risk is identified; the change appears ready for normal checks.

Pre-merge checks: Passed 5
Passed Passed checks: 5 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: awaiting session and cookie writes in the browser client loginAs methods.
Linked Issues check Passed Issue #269 requires both browser login methods to await session and cookie persistence. In src/plugins/japa/browser_client.ts, both context.withGuard(...).loginAs and default context.loginAs use…
Out of Scope Changes check Passed The source change is limited to the Japa browser client login methods described by issue #269. The two added tests directly verify the required completion ordering for session and cookie writes. No un…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed due to a network error.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@thetutlage
thetutlage merged commit 356aede into adonisjs:10.x Oct 16, 2026
1 check passed
@thetutlage

Copy link
Copy Markdown
Member

Thanks for the fix 👍🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

browserContext.loginAs resolves before the session is set

2 participants