Skip to content

[test optimization] Refactor cypress as instrumentation#7829

Draft
juan-fernandez wants to merge 15 commits intomasterfrom
juan-fernandez/refactor-cypress-as-plugin
Draft

[test optimization] Refactor cypress as instrumentation#7829
juan-fernandez wants to merge 15 commits intomasterfrom
juan-fernandez/refactor-cypress-as-plugin

Conversation

@juan-fernandez
Copy link
Copy Markdown
Collaborator

@juan-fernandez juan-fernandez commented Mar 20, 2026

What does this PR do?

Auto-instrument Cypress >=10 so users can run NODE_OPTIONS='-r dd-trace/ci/init' cypress run without manual plugin setup. Two complementary mechanisms:

  1. defineConfig shimmer — wraps defineConfig() via addHook on require('cypress'). Covers CJS configs, TypeScript configs, and programmatic cypress.run().
  2. CLI start() hook — intercepts lib/exec/run.js (Cypress 10-14) and dist/exec/run.js (Cypress 15+) to create a temp ESM wrapper around the config file. Covers plain-object configs (no defineConfig) and ESM .mjs configs.

Support file is auto-injected via a temp wrapper in os.tmpdir() (cleaned up after run). User after:spec/after:run handlers are chained with dd-trace's.

Motivation

Align Cypress with jest, mocha, vitest, playwright, and cucumber — just set NODE_OPTIONS and run.

Changes

  • cypress-config.js (new) — config wrapping logic: wrapConfig, wrapSetupNodeEvents, registerDdTraceHooks, handler chaining, support file injection, mergeReturnedConfig for async setupNodeEvents return values, CLI config file wrapper (always ESM .mjs — works for both CJS and ESM originals). Skips .ts files (Cypress transpiles them internally).
  • cypress.jsdefineConfig shimmer + CLI start() hooks for lib/exec/ and dist/exec/ paths
  • cypress-plugin.jsresetRunState() for programmatic multi-run support, _isInit = false in afterRun
  • ci/cypress/wrap-config.js — CJS bridge for the ESM wrapper to import wrapConfig
  • hooks.js — no special flags needed (removed esmFirst)
  • ci/init.js — unchanged from master

Backwards compatible

  • Old manual plugin (dd-trace/ci/cypress/plugin) still works — _isInit flag prevents double-hooking
  • cypress <10 unaffected (no auto-instrumentation)
  • DD_CONFIG_WRAPPED symbol prevents double-wrapping

Test coverage

9 integration test scenarios, each running for both CJS and ESM module types across Cypress 10.2.0, 14.5.4, and latest:

Test What it covers
Legacy plugin backward compat Manual plugin + defineConfig — _isInit skip
ESM config with NODE_OPTIONS CLI hook creates wrapper for explicit .mjs
Double-run cypress.run() called twice — resetRunState
Plain-object manual module.exports = {} + manual plugin
Plain-object auto (explicit) --config-file plain object, no defineConfig
Plain-object auto (default) No --config-file, default config is plain object
TypeScript config .ts config — skipped by CLI wrapper, handled by defineConfig shimmer
Returned config Async setupNodeEvents returning partial config
Custom after hooks User after:spec/after:run chained with dd-trace

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 20, 2026

Overall package size

Self size: 5.44 MB
Deduped: 6.28 MB
No deduping: 6.28 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.0 | 81.15 kB | 815.98 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 20, 2026

Codecov Report

❌ Patch coverage is 9.42029% with 125 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.01%. Comparing base (f94162c) to head (64bd146).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ges/datadog-instrumentations/src/cypress-config.js 6.95% 107 Missing ⚠️
packages/datadog-instrumentations/src/cypress.js 21.73% 18 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7829      +/-   ##
==========================================
- Coverage   74.26%   74.01%   -0.25%     
==========================================
  Files         765      766       +1     
  Lines       35786    35923     +137     
==========================================
+ Hits        26575    26589      +14     
- Misses       9211     9334     +123     
Flag Coverage Δ
aiguard-macos 39.41% <ø> (-0.10%) ⬇️
aiguard-ubuntu 39.53% <ø> (-0.10%) ⬇️
aiguard-windows 39.20% <ø> (-0.10%) ⬇️
apm-capabilities-tracing-macos 49.15% <ø> (ø)
apm-capabilities-tracing-ubuntu 49.11% <ø> (ø)
apm-capabilities-tracing-windows 48.97% <ø> (ø)
apm-integrations-child-process 38.77% <ø> (-0.10%) ⬇️
apm-integrations-couchbase-18 37.52% <ø> (-0.10%) ⬇️
apm-integrations-couchbase-eol 37.87% <ø> (-0.27%) ⬇️
apm-integrations-oracledb 37.87% <ø> (-0.10%) ⬇️
appsec-express 55.39% <ø> (-0.07%) ⬇️
appsec-fastify 51.72% <ø> (-0.07%) ⬇️
appsec-graphql 51.87% <ø> (-0.08%) ⬇️
appsec-kafka 44.50% <ø> (-0.08%) ⬇️
appsec-ldapjs 44.11% <ø> (-0.08%) ⬇️
appsec-lodash 43.71% <ø> (-0.08%) ⬇️
appsec-macos 58.15% <ø> (-0.07%) ⬇️
appsec-mongodb-core 48.89% <ø> (-0.09%) ⬇️
appsec-mongoose 49.55% <ø> (-0.08%) ⬇️
appsec-mysql 51.08% <ø> (-0.07%) ⬇️
appsec-node-serialize 43.29% <ø> (-0.08%) ⬇️
appsec-passport 47.76% <ø> (-0.09%) ⬇️
appsec-postgres 50.70% <ø> (-0.08%) ⬇️
appsec-sourcing 42.54% <ø> (-0.08%) ⬇️
appsec-stripe 44.73% <ø> (-0.09%) ⬇️
appsec-template 43.46% <ø> (-0.08%) ⬇️
appsec-ubuntu 58.23% <ø> (-0.07%) ⬇️
appsec-windows 57.97% <ø> (-0.07%) ⬇️
instrumentations-instrumentation-bluebird 32.32% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-body-parser 40.63% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-child_process 38.07% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-cookie-parser 34.35% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-express 34.67% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-express-mongo-sanitize 34.48% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-express-session 40.27% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-fs 32.00% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-generic-pool 29.46% <ø> (ø)
instrumentations-instrumentation-http 39.99% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-knex 32.39% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-mongoose 33.51% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-multer 40.38% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-mysql2 38.40% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-passport 44.16% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-passport-http 43.84% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-passport-local 44.37% <ø> (-0.09%) ⬇️
instrumentations-instrumentation-pg 37.84% <ø> (-0.10%) ⬇️
instrumentations-instrumentation-promise 32.25% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-promise-js 32.26% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-q 32.30% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-url 32.22% <ø> (-0.11%) ⬇️
instrumentations-instrumentation-when 32.27% <ø> (-0.11%) ⬇️
llmobs-ai 41.61% <ø> (-0.10%) ⬇️
llmobs-anthropic 40.56% <ø> (-0.09%) ⬇️
llmobs-bedrock 39.32% <ø> (-0.08%) ⬇️
llmobs-google-genai 39.87% <ø> (-0.09%) ⬇️
llmobs-langchain 39.35% <ø> (-0.19%) ⬇️
llmobs-openai 44.09% <ø> (-0.09%) ⬇️
llmobs-vertex-ai 40.13% <ø> (-0.09%) ⬇️
platform-core 31.47% <ø> (ø)
platform-esbuild 34.42% <ø> (ø)
platform-instrumentations-misc 34.11% <ø> (ø)
platform-shimmer 37.56% <ø> (ø)
platform-unit-guardrails 32.89% <ø> (ø)
platform-webpack 19.88% <9.42%> (-0.09%) ⬇️
plugins-azure-durable-functions 25.74% <ø> (ø)
plugins-azure-event-hubs 25.90% <ø> (ø)
plugins-azure-service-bus 25.26% <ø> (ø)
plugins-bullmq 43.61% <ø> (-0.10%) ⬇️
plugins-cassandra 38.02% <ø> (-0.10%) ⬇️
plugins-cookie 26.96% <ø> (ø)
plugins-cookie-parser 26.75% <ø> (ø)
plugins-crypto 26.73% <ø> (ø)
plugins-dd-trace-api 38.43% <ø> (-0.10%) ⬇️
plugins-express-mongo-sanitize 26.89% <ø> (ø)
plugins-express-session 26.70% <ø> (ø)
plugins-fastify 42.36% <ø> (-0.09%) ⬇️
plugins-fetch 38.51% <ø> (-0.10%) ⬇️
plugins-fs 38.75% <ø> (-0.10%) ⬇️
plugins-generic-pool 25.94% <ø> (ø)
plugins-google-cloud-pubsub 45.69% <ø> (-0.09%) ⬇️
plugins-grpc 41.01% <ø> (-0.09%) ⬇️
plugins-handlebars 26.94% <ø> (ø)
plugins-hapi 40.27% <ø> (-0.10%) ⬇️
plugins-hono 40.61% <ø> (-0.10%) ⬇️
plugins-ioredis 38.60% <ø> (-0.10%) ⬇️
plugins-knex 26.57% <ø> (ø)
plugins-langgraph 37.99% <ø> (-0.10%) ⬇️
plugins-ldapjs 24.43% <ø> (ø)
plugins-light-my-request 26.30% <ø> (ø)
plugins-limitd-client 32.60% <ø> (-0.10%) ⬇️
plugins-lodash 26.03% <ø> (ø)
plugins-mariadb 39.66% <ø> (-0.05%) ⬇️
plugins-memcached 38.34% <ø> (-0.10%) ⬇️
plugins-microgateway-core 39.34% <ø> (-0.10%) ⬇️
plugins-moleculer 40.63% <ø> (-0.09%) ⬇️
plugins-mongodb 39.39% <ø> (+0.02%) ⬆️
plugins-mongodb-core 39.12% <ø> (-0.10%) ⬇️
plugins-mongoose 38.92% <ø> (-0.10%) ⬇️
plugins-multer 26.70% <ø> (ø)
plugins-mysql 39.45% <ø> (-0.10%) ⬇️
plugins-mysql2 39.40% <ø> (-0.10%) ⬇️
plugins-node-serialize 27.00% <ø> (ø)
plugins-opensearch 37.74% <ø> (-0.10%) ⬇️
plugins-passport-http 26.76% <ø> (ø)
plugins-postgres 35.57% <ø> (-0.11%) ⬇️
plugins-process 26.73% <ø> (ø)
plugins-pug 26.96% <ø> (ø)
plugins-redis 39.04% <ø> (-0.10%) ⬇️
plugins-router 43.36% <ø> (-0.10%) ⬇️
plugins-sequelize 25.55% <ø> (ø)
plugins-test-and-upstream-amqp10 38.47% <ø> (-0.25%) ⬇️
plugins-test-and-upstream-amqplib 44.38% <ø> (-0.09%) ⬇️
plugins-test-and-upstream-apollo 39.24% <ø> (-0.09%) ⬇️
plugins-test-and-upstream-avsc 38.69% <ø> (-0.10%) ⬇️
plugins-test-and-upstream-bunyan 33.93% <ø> (-0.10%) ⬇️
plugins-test-and-upstream-connect 40.93% <ø> (-0.10%) ⬇️
plugins-test-and-upstream-graphql 40.28% <ø> (-0.10%) ⬇️
plugins-test-and-upstream-koa 40.52% <ø> (-0.10%) ⬇️
plugins-test-and-upstream-protobufjs 38.92% <ø> (-0.10%) ⬇️
plugins-test-and-upstream-rhea 44.40% <ø> (-0.10%) ⬇️
plugins-undici 39.36% <ø> (-0.09%) ⬇️
plugins-url 26.73% <ø> (ø)
plugins-valkey 38.31% <ø> (-0.10%) ⬇️
plugins-vm 26.73% <ø> (ø)
plugins-winston 34.26% <ø> (-0.10%) ⬇️
plugins-ws 42.12% <ø> (-0.10%) ⬇️
profiling-macos 40.65% <ø> (-0.10%) ⬇️
profiling-ubuntu 40.78% <ø> (-0.10%) ⬇️
profiling-windows 41.88% <ø> (-0.52%) ⬇️
serverless-azure-functions-client 25.62% <ø> (ø)
serverless-azure-functions-eventhubs 25.62% <ø> (ø)
serverless-azure-functions-servicebus 25.62% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@datadog-datadog-prod-us1
Copy link
Copy Markdown

datadog-datadog-prod-us1 bot commented Mar 20, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 8.70%
Overall Coverage: 68.49% (-0.22%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 64bd146 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@pr-commenter
Copy link
Copy Markdown

pr-commenter bot commented Mar 20, 2026

Benchmarks

Benchmark execution time: 2026-03-30 13:54:00

Comparing candidate commit ba4b9ae in PR branch juan-fernandez/refactor-cypress-as-plugin with baseline commit b6d42d8 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 227 metrics, 33 unstable metrics.

@juan-fernandez juan-fernandez changed the title [testing] Refactor cypress as instrumentation [test optimization] Refactor cypress as instrumentation Mar 25, 2026
juan-fernandez and others added 3 commits March 25, 2026 13:41
… test

On CI, early-flush payloads may contain test events without per-level
metadata keys, causing the metadata?.test filter to exclude them.
Use a simpler event.type === 'test' filter instead.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…c chaining

- Support file injection uses a temp wrapper in os.tmpdir() with absolute
  paths instead of mutating the user's support file on disk. Cleaned up
  via after:run handler.
- cypressPlugin.init() Promise is now properly returned so Cypress awaits
  async config mutations (retries, library config).
- User's after:spec/after:run handlers are chained with dd-trace's,
  awaiting each in sequence (supports async handlers returning Promises).
- Also wraps cypress.open() for interactive mode.
- Handles async setupNodeEvents (Promise return) from user code.
- Adds integration test verifying support file is not modified and
  wrapper is cleaned up.
- Adds CODEOWNERS for new config files.
- Removes stale Module._load approach (overwritten by dd-trace's ritm).
- Updates ESM config to plain object (wrapped via cypress.run hook).
- Fixes legacy plugin config comment.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@juan-fernandez juan-fernandez marked this pull request as ready for review March 30, 2026 13:46
@juan-fernandez juan-fernandez requested a review from a team as a code owner March 30, 2026 13:46
gnufede
gnufede previously approved these changes Mar 30, 2026
@juan-fernandez juan-fernandez requested a review from a team as a code owner April 1, 2026 09:56
@juan-fernandez juan-fernandez marked this pull request as draft April 1, 2026 10:09
juan-fernandez and others added 3 commits April 1, 2026 21:30
- Remove `ensureCiVisibilityTracer` duplication — `ci/init` now initializes
  the tracer in Cypress CLI/Electron processes (removed `isCliTool` skip)
- Remove `esmFirst` flag — `defineConfig` shimmer works for ESM configs via
  ritm because Cypress internally transpiles .mjs to CJS
- Remove `rewriteCliNodeOptions` — config wrapper now lives next to the
  original file instead of in os.tmpdir(), so module resolution works naturally
- Remove `run`/`open` shimming — inline config wrapping was belt-and-suspenders
  since Cypress never calls setupNodeEvents from inline config
- Always use ESM (.mjs) wrappers for both config and support files — ESM can
  import both CJS and ESM, so it works regardless of "type":"module"
- Fix CLI hook file paths: lib/exec/run.js → dist/exec/run.js (actual Cypress
  package structure), handle .default on transpiled module exports
- Add ESM variants for all integration tests so every scenario runs for both
  CJS and ESM module types
- Make returned-config test async to cover the promise branch in
  wrapSetupNodeEvents

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Hook both lib/exec/{run,open}.js (Cypress 10-14) and dist/exec/{run,open}.js
  (Cypress 15+) so the CLI wrapper fires across all supported versions
- Add ESM variants of all integration test configs and scripts so every
  scenario runs for both CJS and ESM module types
- Add plain-object auto-instrumentation test (no defineConfig, no manual plugin)
- Fix ESM config imports to include .js extension for dd-trace/ci/cypress/plugin
- Revert ci/init.js to master (no isCliTool/ensureCiVisibilityTracer needed)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Resolve default config file when no --config-file is passed, enabling
  plain-object configs (without defineConfig) in the standard cypress run flow
- Skip .ts config files — Cypress transpiles them internally, ESM wrapper can't
  import .ts directly
- Hook both lib/exec/{run,open}.js (Cypress 10-14) and dist/exec/{run,open}.js
  (Cypress 15+) so the CLI wrapper fires across all supported versions
- Respect configFile: false — don't override with a resolved default
- Add comments to programmatic test scripts clarifying that instrumentation
  works via the default config file, not inline configs
- Add TypeScript config regression test
- Add plain-object default config (no --config-file) regression test
- Fix ESM config imports to include .js extension for dd-trace/ci/cypress/plugin

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@juan-fernandez juan-fernandez force-pushed the juan-fernandez/refactor-cypress-as-plugin branch from f9b4c02 to 92b02d4 Compare April 1, 2026 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants