Skip to content

👷 [RUM-19777] Run BrowserStack unit tests with Vitest (webdriverio provider) - #5136

Merged
thomas-lebeau merged 13 commits into
mainfrom
thomas.lebeau/vitest-wdio-bs-spike
Nov 19, 2026
Merged

thomas-lebeau merged 13 commits into
mainfrom
thomas.lebeau/vitest-wdio-bs-spike

Conversation

@thomas-lebeau

@thomas-lebeau thomas-lebeau commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Every previous Vitest attempt stalled on BrowserStack: Vitest's default Playwright provider can't pick exact browser versions or drive real Android devices, and Vitest's client needs Chrome 87+ / Safari 15.4+, while our unit matrix is Chrome 80, Edge 80, Firefox 78, Safari 14 and Android Chrome.

This PR moves unit-bs to Vitest using the webdriverio provider, which reaches that exact matrix on real BrowserStack devices. Stacked on #5135. The Karma BrowserStack config is still in the repo (unused) so reverting is a one-line change; it is deleted in a follow-up PR.

Closes RUM-19777

Changes

  • yarn test:unit:bs (CI unit-bs matrix) runs vitest run --config test/unit-vitest/vite.bs.config.ts.
  • test/unit-vitest/vite.bs.config.ts: webdriverio provider against hub.browserstack.com:
    • W3C Selenium (seleniumVersion: 4.0.0) on desktop browsers: BrowserStack otherwise serves Chrome 80 through Selenium 2.53, which rejects the window resize Vitest sends before each file. Viewport errors are non-fatal anyway.
    • WebDriver keepalive (+ idleTimeout: 300): Vitest talks to the page over its own WebSocket, so BrowserStack otherwise kills the session as idle.
    • Downlevel to ES2020: Vitest's browser client ships ||=/??=. A plugin transpiles dependency JS and routes Vitest's prebuilt /__vitest__/ assets through the Vite pipeline (@oxc-project/runtime for injected helpers).
    • Polyfills for the orchestrator and tester pages: Object.hasOwn, Array.prototype.at, … (from 👷 Migrate unit tests to Vitest #4874), a same-origin BroadcastChannel (Safari < 15.4) and AggregateError (Firefox < 79).
    • bs-local.com for Safari only (BrowserStack rewrites localhost there); other browsers keep localhost, a secure context needed by secure cookie tests.
    • Page error reporter to the terminal (Safari on BrowserStack exposes no console logs).
    • verbose reporter (the BrowserStack wrapper kills silent runs), per-browser JUnit file, no coverage, same exclusions as karma.bs.conf.js.
  • trackRuntimeError.spec: stop() resolved to window.stop(), which made Firefox 78 reload the test iframe; it now unsubscribes from the observable.
  • webdriverio 9 → 10: v9's bundled undici 6 fails every request on Node 26 (UND_ERR_INVALID_ARG).

Results

All five unit-bs jobs pass the full suite. Job durations (CI Visibility, minutes, single run):

Job Karma avg / p50 / p90 (other PRs, last 14 days) Vitest (this PR)
unit-bs Chrome 80 3.19 / 2.98 / 4.28 5.42
unit-bs Android Chrome 3.04 / 2.81 / 3.95 6.62
unit-bs Edge 80 3.10 / 2.87 / 4.03 4.45 (on retry, see below)
unit-bs Firefox 78 3.13 / 3.04 / 3.79 3.97
unit-bs Safari 14 5.25 / 5.33 / 6.51 3.67

Open issues

  • Reliability: Vitest doesn't reconnect a lost browser (Karma had browserDisconnectTolerance). We saw one BrowserStack tunnel drop and one Edge connect timeout. Planned: bs-wrapper.ts exits with a dedicated code on these infrastructure errors + GitLab retry: exit_codes on unit-bs.
  • Speed: Chrome 80 and Android are 2–3.5 min slower than Karma (single runs; to be confirmed and investigated, likely modules served one by one through the tunnel).

⚠️ Written with an AI agent (Claude Code).

Test instructions

Needs BS_USERNAME / BS_ACCESS_KEY (e.g. in .env):

BS_BROWSER=safari-desktop yarn test:unit:bs

BS_BROWSER is one of edge, firefox, safari-desktop, chrome-desktop, chrome-mobile.

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum Salesforce 192.85 KiB 192.85 KiB 0 B 0.00% ✅
Rum 192.82 KiB 192.82 KiB 0 B 0.00% ✅
Rum Salesforce Profiler 8.01 KiB 8.01 KiB 0 B 0.00% ✅
Rum Profiler 8.01 KiB 8.01 KiB 0 B 0.00% ✅
Rum Salesforce Recorder 32.62 KiB 32.62 KiB 0 B 0.00% ✅
Rum Recorder 32.62 KiB 32.62 KiB 0 B 0.00% ✅
Logs 60.25 KiB 60.25 KiB 0 B 0.00% ✅
Rum Slim 148.67 KiB 148.67 KiB 0 B 0.00% ✅
Worker 24.23 KiB 24.23 KiB 0 B 0.00% ✅
Rum Shopify 224.18 KiB 224.18 KiB 0 B 0.00% ✅
Rum Shopify Profiler 8.00 KiB 8.00 KiB 0 B 0.00% ✅
Rum Shopify Recorder 4.85 KiB 4.85 KiB 0 B 0.00% ✅

@thomas-lebeau
thomas-lebeau added this pull request to stack #5137 October 13, 2026 15:42
@datadog-prod-us1-3

datadog-prod-us1-3 Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🔁 Datadog re-ran unit-bs: [edge] after it failed. The re-run passed. View in Datadog

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 77.93% (+0.03%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: d0beb4b | Docs | View more details | Give us feedback!

@thomas-lebeau
thomas-lebeau force-pushed the thomas.lebeau/vitest-wdio-bs-spike branch 3 times, most recently from 8d63fc5 to 8ad2817 Compare October 14, 2026 11:22
@thomas-lebeau
thomas-lebeau force-pushed the thomas.lebeau/vitest-wdio-bs-spike branch from 8ad2817 to 80c77b4 Compare October 14, 2026 11:54
@thomas-lebeau thomas-lebeau changed the title ✅ [spike] Run Vitest unit tests on BrowserStack with the webdriverio provider 👷 Run BrowserStack unit tests with Vitest (webdriverio provider) Oct 14, 2026
@thomas-lebeau
thomas-lebeau force-pushed the thomas.lebeau/vitest-wdio-bs-spike branch from 80c77b4 to 840afce Compare October 15, 2026 14:37
@thomas-lebeau
thomas-lebeau force-pushed the thomas.lebeau/vitest-wdio-bs-spike branch 10 times, most recently from 728322c to 4c500f2 Compare November 5, 2026 06:38
@thomas-lebeau thomas-lebeau changed the title 👷 Run BrowserStack unit tests with Vitest (webdriverio provider) 👷 [RUM-19777] Run BrowserStack unit tests with Vitest (webdriverio provider) Nov 5, 2026
@thomas-lebeau
thomas-lebeau force-pushed the thomas.lebeau/vitest-wdio-bs-spike branch 4 times, most recently from bafed24 to aac904a Compare November 11, 2026 05:36
Base automatically changed from thomas.lebeau/vitest-wip to main November 12, 2026 06:03
@thomas-lebeau
thomas-lebeau force-pushed the thomas.lebeau/vitest-wdio-bs-spike branch from aac904a to 3b57986 Compare November 12, 2026 06:03
@thomas-lebeau
thomas-lebeau marked this pull request as ready for review November 12, 2026 06:03
@thomas-lebeau
thomas-lebeau requested a review from a team as a code owner November 12, 2026 06:03
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T10:47:28.370876Z d0beb4b New commits
🔒 Security Review ✅ Completed 2026-11-19T10:49:31.395980Z d0beb4b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: FAIL

The default full-matrix run applies Safari’s hostname to Chromium too, causing secure-cookie tests to fail on Android Chrome 145+. Individually selected Chromium runs retain localhost and avoid this defect.

Open Bits AI session

🤖 Bits Code Review · Commit 3b57986

Comment thread test/unit-vitest/vite.bs.config.ts Resolved
@thomas-lebeau
thomas-lebeau force-pushed the thomas.lebeau/vitest-wdio-bs-spike branch from 3b57986 to 733adf8 Compare November 12, 2026 15:37

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: FAIL

Running the documented full BrowserStack matrix without BS_BROWSER applies Safari’s insecure hostname to every browser, suppressing CookieStore coverage and breaking secure-cookie assertions on Android Chrome 145+.

Open Bits AI session

🤖 Bits Code Review · Commit 733adf8

Comment thread test/unit-vitest/vite.bs.config.ts Resolved
Comment thread package.json Resolved
Comment thread test/unit-vitest/vite.bs.config.ts Resolved
Comment thread test/unit-vitest/vite.bs.config.ts Resolved
thomas-lebeau and others added 13 commits November 19, 2026 10:37
Spike: check whether Vitest Browser Mode can cover our full BrowserStack
matrix (Chrome 80, Edge 80, Firefox 78, Safari 14, Android Chrome), which
the playwright provider can't.

- vite.bs.config.ts: webdriverio provider against hub.browserstack.com,
  with a WebDriver keepalive (Vitest uses its own WebSocket, so
  BrowserStack otherwise kills the session as idle)
- downlevel dependencies and Vitest's static client assets to ES2020
- polyfills (Object.hasOwn, BroadcastChannel, ...) for orchestrator and
  tester pages
- serve from bs-local.com so Safari can reach the server
- report page errors to the terminal (Safari exposes no console logs)
- webdriverio 9 -> 10 (v9's bundled undici 6 breaks on Node 26)

browser-core results: all 72 files pass on Chrome 80, Edge 80, Safari 14
and Android Chrome; Firefox 78 passes 71/72 (trackRuntimeError.spec.ts
triggers an iframe reload when run as a whole).
Point `yarn test:unit:bs` (CI `unit-bs` matrix) at the webdriverio
BrowserStack config, to see where the approach stands on the full suite.

- verbose reporter, so the BrowserStack wrapper doesn't kill silent runs,
  and a per-browser junit file in test-report/unit-bs
- no coverage on BrowserStack
- same exclusions as karma.bs.conf.js (developer extension everywhere,
  Angular on Chrome/Edge 80 and Firefox 78)

Files stay isolated (one iframe per file): running without isolation
currently fails 151 tests (state leaking across files, shim gaps).
BrowserStack served Chrome 80 through Selenium 2.53 (JSON Wire protocol),
which rejects the `POST /window/rect` Vitest sends before each test file.
Also make a failed viewport resize non-fatal.
- trackRuntimeError.spec: `stop()` resolved to `window.stop()`, which made
  Firefox 78 reload the Vitest test iframe. Unsubscribe from the observable
  instead.
- polyfill AggregateError (Firefox < 79), used by Vitest to report errors
- only serve from bs-local.com on Safari: other browsers need `localhost`
  (a secure context) for `secure` cookies (cookieAccess.spec on Android)
The error reporter sent its reports with the page's `XMLHttpRequest`, which specs
instrument (ex: `xhrObservable`). Reporting the errors `trackRuntimeError.spec.ts`
throws on purpose therefore added `loadend` listeners through the SDK
instrumentation, that the leak detection reported as leaked once the spec ended,
failing the tests on every BrowserStack browser (and reporting its own errors in
a loop). Save `open` and `send` when the reporter loads, before any spec runs,
and use them instead.

Environment: Datadog workspace

Co-Authored-By: Claude Code claude-opus-5-5 <noreply@anthropic.com>
`forEach.ts` restores the page URL after each test, so going back to the original
pathname in `datadogProfiler.spec.ts`'s `afterEach` is redundant. It also always
changed the URL (dropping Vitest's query string), making `forEach.ts` restore it
again: about 80 history changes for this spec, close to Safari's limit of 100 per
30 seconds, which made Safari on BrowserStack fail with "Attempt to use
history.pushState() more than 100 times per 30 seconds" depending on the test
order. This spec now makes 6.

Environment: Datadog workspace

Co-Authored-By: Claude Code claude-opus-5-5 <noreply@anthropic.com>
Some BrowserStack runs fail with "The iframe was reloaded during a test" (5
times on Chrome Android, Edge and Safari over the last 2 days, in different
specs), and the logs don't tell what reloads the page: no Vite reload is
logged and no spec navigates.

Make the error reporter also report, for each page load, the navigation type
(`reload`, `navigate` or `back_forward`), `document.wasDiscarded` and the full
URL, and the `beforeunload`, `pagehide`, `freeze` and `visibilitychange` events
fired by the browser (events dispatched by specs are ignored). Events fired
while the page unloads are sent with `sendBeacon`, saved before the tests run.

Environment: Datadog workspace

Co-Authored-By: Claude Code claude-opus-5-5 <noreply@anthropic.com>
The page lifecycle report showed that, when a BrowserStack run fails with "The
iframe was reloaded during a test", the test iframe is reloaded in place
(`beforeunload`, then `navigation=reload` with the same URL, not discarded).
Vite's client, loaded in the test iframe even with `hmr: false`, calls
`location.reload()` once its WebSocket closes and the server answers again,
which would happen when the BrowserStack tunnel drops the connection.

Report when the WebSockets to the test server (Vite's and Vitest's own) open,
error and close, with the close code and reason, to confirm it and see which
connections drop. Access tokens are masked.

Environment: Datadog workspace

Co-Authored-By: Claude Code claude-opus-5-5 <noreply@anthropic.com>
Vite's client is loaded in the test pages even though Vitest turns HMR off, and
keeps a WebSocket to the server. On BrowserStack, that connection gets dropped
(code 1006, detected when the client sends its ping every 30s: after 60, 90 and
120s in the reported runs) while Vitest's own WebSocket stays up, and the client
then reloads the page once the server answers again. This failed runs with "The
iframe was reloaded during a test" on every browser, in random specs.

Unlike Vitest's, nothing is ever sent to the browser on Vite's WebSocket, so it
is likely dropped for inactivity: send a keepalive message on it every 15s. And
patch Vite's client so it doesn't reload the page (nor log) if it is dropped
anyway; the error reporter still reports the WebSocket closing. The patch throws
if a Vite upgrade changes the code it targets.

Environment: Datadog workspace

Co-Authored-By: Claude Code claude-opus-5-5 <noreply@anthropic.com>
They were added to find what reloaded the test page on BrowserStack (Vite's
client, after its WebSocket got dropped), which is now fixed. Keep reporting
page errors.

Environment: Datadog workspace

Co-Authored-By: Claude Code claude-opus-5-5 <noreply@anthropic.com>
…ck videos

- Polyfill `String.prototype.replaceAll`: Vitest's pretty-format uses it to
  print assertion diffs, and Chrome/Edge 80 don't have it.
- Pin Vite to 8.3.7 in the root and the developer extension, the version
  Vitest resolves, so the lockfile has a single Vite.
- Lower `connectTimeout` to 4 minutes, under the BrowserStack wrapper's
  5-minute no-output timeout, so Vitest reports a failed connection instead
  of being killed.
- Turn off BrowserStack video recording, as the Karma setup did.

Environment: Datadog workspace

Co-Authored-By: Claude Code claude-opus-5-5 <noreply@anthropic.com>
@thomas-lebeau
thomas-lebeau force-pushed the thomas.lebeau/vitest-wdio-bs-spike branch from 733adf8 to d0beb4b Compare November 19, 2026 10:45

@datadog-prod-us1-3 datadog-prod-us1-3 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: FAIL

The default full-matrix invocation applies Safari’s HTTP hostname to every browser, breaking secure-cookie expectations and reducing CookieStore coverage on recent Android Chrome.

Open Bits AI session

🤖 Bits Code Review · Commit d0beb4b

// BrowserStack rewrites localhost to bs-local.com for Safari: serve from that host directly
// (it resolves to 127.0.0.1) so the page is reachable and cookies match the page host. Other
// browsers keep `localhost`, which is a secure context (needed e.g. for `secure` cookies).
api: { host: configurations.some(({ name }) => name === 'Safari') ? 'bs-local.com' : 'localhost' },

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Scope the Safari hostname to Safari browsers

Running yarn test:unit:bs without BS_BROWSER includes Safari, so this shared setting serves every browser from HTTP bs-local.com. Android Chrome consequently loses its localhost secure context: on Chrome 145+, the cookieAccess test expecting both ordinary and Secure Partitioned cookies fails because the secure cookie is rejected, and CookieStore coverage is skipped. Preserve localhost for non-Safari browsers through browser-specific projects/servers or equivalent URL handling; the API hostname cannot simply be overridden on an individual Vitest browser instance.

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🤖 Bits Code Review · Open Bits AI session

@thomas-lebeau
thomas-lebeau merged commit c62d098 into main Nov 19, 2026
31 checks passed
@thomas-lebeau
thomas-lebeau deleted the thomas.lebeau/vitest-wdio-bs-spike branch November 19, 2026 11:11
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants