Skip to content

chore: Add code comments + refactor if statement for when snapshots are taken#34026

Merged
jennifer-shehane merged 9 commits into
developfrom
claude/headless-cypress-perf-B94TM
Jun 6, 2026
Merged

chore: Add code comments + refactor if statement for when snapshots are taken#34026
jennifer-shehane merged 9 commits into
developfrom
claude/headless-cypress-perf-B94TM

Conversation

@jennifer-shehane
Copy link
Copy Markdown
Member

@jennifer-shehane jennifer-shehane commented Jun 5, 2026

Summary

This PR is documentation plus a readability refactor — no functional or behavioral change. It clarifies the per-command snapshot gate and documents a redundant DOM clone in headless Test Replay runs. The actual optimization (skipping the clone) is being handled on the Test Replay capture service side, so the driver's runtime behavior here is intentionally left unchanged.

Background

In a headless cypress run recorded with Test Replay, Log.snapshot() calls cy.createSnapshot for every command, which clones the entire DOM <body> (createSnapshotBodydocument.importNode). That clone is effectively wasted for Test Replay:

  • The command-log snapshot's body/htmlAttrs are dropped at ingestion — only name/timestamp/elementsToHighlight are consumed.
  • The replayed DOM is reconstructed from a separate capture-protocol path (cy:protocol-snapshotdom:full-snapshot), not from this clone.
  • The capture protocol may even replace cy.createSnapshot outright when numTestsKeptInMemory === 0, depending on testing type.

Because skipping the clone must be coordinated with (and may already be handled by) the Test Replay capture service, this PR leaves runtime behavior unchanged and instead documents the situation where a future reader would notice the redundant work.

What changed

  • packages/driver/src/cypress/log.ts — Rewrote the snapshot() bail condition. The previous A || (B || C) && D form relied on && binding tighter than ||, which was easy to misread. It is now expressed with named booleans — shouldDeferSnapshotToSpecBridge, needsTimeTravelSnapshot, needsProtocolSnapshot, and shouldTakeSnapshot — so the intent reads directly: take a snapshot only when something will consume it (interactive command-log time-travel, or Test Replay capture), and defer cross-origin logs to the spec bridge. Behavior is identical — verified equivalent to the original across every combination of cross-origin / isInteractive / numTestsKeptInMemory / isProtocolEnabled.
  • packages/driver/src/cy/snapshots.ts — Added a Test Replay note above createSnapshot documenting that the importNode body clone runs per command during protocol runs even though the resulting body is discarded downstream.

Behavioral impact

None. No config, public API, command-log, or Test Replay output changes. Open Mode time-travel, cross-origin snapshots, and protocol capture all behave exactly as before.

Steps to test

There is nothing functional to exercise — the gate refactor is behavior-preserving and the comments are documentation. Existing driver snapshot specs continue to pass.

How has the user experience changed?

No user-facing change. This is internal documentation plus a readability refactor. (The actual performance win lands when the redundant clone is removed, which is being handled on the Test Replay capture service side.)

PR Tasks

  • Is there an associated issue with maintainer approval for PR submission?
  • Have tests been added/updated? (N/A — no functional change; the refactor is behavior-preserving)
  • Has a PR for user-facing changes been opened in cypress-documentation? (N/A)
  • Have API changes been updated in the type definitions? (N/A — no public API changes)

https://claude.ai/code/session_017CFfoAmczKsjQFduBMHf82


Note

Low Risk
Readability and documentation only; snapshot gating logic is preserved and now covered by explicit unit tests.

Overview
Refactors Log.snapshot() so when snapshots run is expressed with named flags (shouldDeferSnapshotToSpecBridge, needsTimeTravelSnapshot, needsProtocolSnapshot, shouldTakeSnapshot) instead of a hard-to-read || / && mix. Intent is unchanged: snapshot only for command-log time-travel (interactive + tests kept in memory) or protocol/Test Replay, and skip on the primary origin for cross-origin logs.

Documents in createSnapshot that headless Test Replay still triggers per-command importNode body clones whose body/htmlAttrs are not used downstream (ingestion drops them; replay DOM comes from protocol capture).

Adds unit tests in log.spec.ts that lock in snapshot gating for headless vs open mode, protocol on/off, memory retention, and cross-origin / spec-bridge cases.

Reviewed by Cursor Bugbot for commit 00b7aeb. Bugbot is set up for automated code reviews on this repo. Configure here.

claude and others added 3 commits June 5, 2026 03:56
…ss Test Replay

In a headless run with the capture protocol enabled, the driver cloned the
entire AUT <body> (document.importNode + stylesheet collection + iframe
replacement) on every command via Log.snapshot() -> createSnapshot(). Those
captured body/htmlAttrs are dropped at protocol ingestion and never used: Test
Replay reconstructs the DOM from its own cy:protocol-snapshot ->
dom:full-snapshot path, so this was wasted per-command work.

Emit a metadata-only snapshot (name/timestamp) in that case while still
toggling the highlight attribute so the protocol can resolve
elementsToHighlight. Gate on !isInteractive && isProtocolEnabled &&
numTestsKeptInMemory === 0 so Open Mode (e.g. Studio AI) and the driver
snapshot tests (numTestsKeptInMemory: 1) keep full-body snapshots. The two
serialization short-circuits now share the same predicate.

https://claude.ai/code/session_017CFfoAmczKsjQFduBMHf82
cypress-bot[bot]
cypress-bot Bot previously approved these changes Jun 5, 2026
@cypress-bot cypress-bot Bot dismissed their stale review June 5, 2026 14:23

New commits pushed (auto-approval was for 49118d3, head is now cd53dfe). Auto-approval dismissed pending Cursor Bugbot re-review of the new head SHA.

cypress-bot[bot]
cypress-bot Bot previously approved these changes Jun 5, 2026
@jennifer-shehane jennifer-shehane changed the title Skip DOM clone in headless protocol runs for performance perf: Skip DOM clone in headless protocol runs for performance Jun 5, 2026
@cypress
Copy link
Copy Markdown

cypress Bot commented Jun 5, 2026

cypress    Run #71520

Run Properties:  status check passed Passed #71520  •  git commit 00b7aeb9c9: Merge branch 'develop' into claude/headless-cypress-perf-B94TM
Project cypress
Branch Review claude/headless-cypress-perf-B94TM
Run status status check passed Passed #71520
Run duration 18m 52s
Commit git commit 00b7aeb9c9: Merge branch 'develop' into claude/headless-cypress-perf-B94TM
Committer Jennifer Shehane
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 6
Tests that did not run due to a developer annotating a test with .skip  Pending 1096
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 24806
View all changes introduced in this branch ↗︎

Warning

No Report: Something went wrong and we could not generate a report for the Application Quality products.

Reverts the runtime change that skipped the per-command DOM body-clone in
headless Test Replay runs, since the capture protocol may replace
cy.createSnapshot under numTestsKeptInMemory === 0 (handled on the Test
Replay/consumer side). Leaves explanatory comments at the snapshot gate in
cypress/log.ts and at createSnapshotBody in cy/snapshots.ts so the redundant
clone is noticeable to anyone optimizing this path. Also removes the no-longer
-applicable changelog entry.

https://claude.ai/code/session_017CFfoAmczKsjQFduBMHf82
@cypress-bot cypress-bot Bot dismissed their stale review June 5, 2026 20:45

New commits pushed (auto-approval was for cd53dfe, head is now a0824d9). Auto-approval dismissed pending Cursor Bugbot re-review of the new head SHA.

cypress-bot[bot]
cypress-bot Bot previously approved these changes Jun 5, 2026
Replaces the precedence-sensitive `A || (B || C) && D` snapshot gate with named
boolean locals (isCrossOriginLogOnPrimary / reporterWillUseSnapshot /
protocolWillUseSnapshot) and explicit grouping. Behavior is identical (verified
across all input combinations); only readability changes.

https://claude.ai/code/session_017CFfoAmczKsjQFduBMHf82
@cypress-bot cypress-bot Bot dismissed their stale review June 5, 2026 20:54

New commits pushed (auto-approval was for a0824d9, head is now 51c7cbe). Auto-approval dismissed pending Cursor Bugbot re-review of the new head SHA.

cypress-bot[bot]
cypress-bot Bot previously approved these changes Jun 5, 2026
@cypress-bot cypress-bot Bot dismissed their stale review June 5, 2026 21:04

New commits pushed (auto-approval was for 51c7cbe, head is now d018793). Auto-approval dismissed pending Cursor Bugbot re-review of the new head SHA.

cypress-bot[bot]
cypress-bot Bot previously approved these changes Jun 5, 2026
@cypress-bot cypress-bot Bot dismissed their stale review June 5, 2026 21:07

Cursor Bugbot risk assessment is no longer Low Risk. Auto-approval dismissed; manual review required.

@jennifer-shehane jennifer-shehane changed the title perf: Skip DOM clone in headless protocol runs for performance chore: Add code comments + refactor if statement for when snapshots are taken Jun 5, 2026
Covers the refactored snapshot() bail condition across headless/Open Mode,
protocol on/off, in-memory retention, and cross-origin/spec-bridge cases by
asserting whether createSnapshot is invoked. Verified to fail under a mutated
gate (mutation testing).

https://claude.ai/code/session_017CFfoAmczKsjQFduBMHf82
@jennifer-shehane jennifer-shehane merged commit 06f6d07 into develop Jun 6, 2026
87 of 88 checks passed
@jennifer-shehane jennifer-shehane deleted the claude/headless-cypress-perf-B94TM branch June 6, 2026 12:39
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.

3 participants