Skip to content

Conversation

@thomas-lebeau
Copy link
Collaborator

@thomas-lebeau thomas-lebeau commented Feb 11, 2026

Motivation

The e2e service worker test infrastructure was tightly coupled and hard to extend. Adding new SDK configurations (e.g. RUM in a worker) required modifying multiple options flags (importScript, nativeLog) and threading them through the test framework. The interactWithWorker API forced tests to go through postMessage with the active service worker, making test code indirect and harder to read.

Changes

  • New createWorker() builder (test/e2e/lib/framework/createWorker.ts): Composable builder pattern that mirrors the existing createTest() API. Workers can be configured with .withRum() and .withLogs() independently, replacing the previous boolean flags.
  • Replace interactWithWorker with evaluateInWorker: Instead of sending messages to the worker via postMessage, evaluateInWorker sends code as a string and executes it via new Function() inside the worker. This allows tests to call SDK APIs (e.g. DD_LOGS.logger.log(...)) directly in the worker scope.
  • Dynamic worker script generation: workerSetup() in pageSetups.ts now conditionally includes Logs and/or RUM SDK imports based on the worker configuration, and always registers a message listener for the evaluate protocol.
  • More explicit tests: Each test now specifies its own Logs/RUM configuration, so options like forwardConsoleLogs are only enabled in the test that actually uses console.log, making the intent clearer.
  • New RUM-in-worker tests (test/e2e/scenario/rum/init.scenario.ts): Verifies that initializing RUM inside a service worker gracefully warns about missing session storage instead of crashing.
  • Updated existing logs worker tests to use the new createWorker().withLogs() builder and evaluateInWorker API.

Test instructions

yarn test:e2e:init
yarn test:e2e -g "service worker"

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

Introduce `createWorker()` builder with `.withRum()` and `.withLogs()` methods,
replacing inline worker configuration in `createTest`. This makes worker setup
composable and enables testing RUM and Logs independently in service workers.
…xecution in service workers

Add a generic evaluate message handler in the worker that executes arbitrary
code sent from tests, enabling direct SDK API calls (DD_LOGS, DD_RUM) inside
the worker. Remove the nativeLog option in favor of explicit console.log calls.
@cit-pr-commenter-54b7da
Copy link

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 169.44 KiB 169.44 KiB 0 B 0.00%
Rum Profiler 4.31 KiB 4.31 KiB 0 B 0.00%
Rum Recorder 24.54 KiB 24.54 KiB 0 B 0.00%
Logs 56.72 KiB 56.72 KiB 0 B 0.00%
Flagging 944 B 944 B 0 B 0.00%
Rum Slim 126.26 KiB 126.26 KiB 0 B 0.00%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance

Pending...

🧠 Memory Performance

Pending...

🔗 RealWorld

@thomas-lebeau thomas-lebeau marked this pull request as ready for review February 11, 2026 15:40
@thomas-lebeau thomas-lebeau requested a review from a team as a code owner February 11, 2026 15:40
@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Feb 11, 2026

⚠️ Tests

Fix all issues with Cursor

⚠️ Warnings

🧪 1 Test failed

Service workers Rum › service worker with worker rum - importScripts from rum/init.scenario.ts (Datadog) (Fix with Cursor)
createTest.ts:227:3 service worker with worker rum - importScripts

[Firefox] › ../lib/framework/createTest.ts:227:3 › Service workers Rum › service worker with worker rum - importScripts 

    Error: expect(received).toHaveLength(expected)

    Expected length: 1
    Received length: 0
    Received array:  []

...

ℹ️ Info

❄️ No new flaky tests detected

🎯 Code Coverage
Patch Coverage: 100.00%
Overall Coverage: 77.25% (-0.01%)

View detailed report

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 1ae678c | Docs | Datadog PR Page | Was this helpful? Give us feedback!

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d2faf21dc6

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

await page.evaluate(`(${cb.toString()})(window.myServiceWorker.active)`)
evaluateInWorker: async (fn: () => void) => {
await page.evaluate((code) => {
window.myServiceWorker.active?.postMessage({ __type: 'evaluate', code })

Choose a reason for hiding this comment

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

P2 Badge Fail fast when service worker is not yet active

evaluateInWorker() posts messages through window.myServiceWorker.active?.postMessage, which silently no-ops when registration.active is still null (a common state right after first registration). In that case the helper resolves even though the worker callback never runs, so service-worker tests can become flaky or give misleading results because assertions run without the intended worker-side action.

Useful? React with 👍 / 👎.

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.

1 participant