Re-implementing snapshot testing in cypress, as none of the dependencies I found are maintained and working#4039
Conversation
…ies I found are maintained and working
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
💤 Files with no reviewable changes (1)
📝 WalkthroughWalkthroughSnapshot testing was migrated from an external package to an in-repo solution: snapshots converted from JS to JSON, Cypress tasks for reading/writing/checking snapshots were added, and a new toMatchSnapshot command drives read/write/compare via snapshots.json. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@cypress.config.js`:
- Around line 49-67: The snapshot read/write logic (readSnapshot,
snapshotExists, writeSnapshot using snapshotsPath) is vulnerable to TOCTOU
races; replace the separate existsSync+read sequences with a single helper like
loadSnapshots() that reads/parses the file (returning {} if missing) and then
serialize all writes using an in-process mutex/Promise queue so concurrent tasks
enqueue read-modify-write operations rather than racing; update snapshotExists
and readSnapshot to call loadSnapshots(), and make writeSnapshot perform
loadSnapshots(), merge the new key/value, then write the file while holding the
queue lock to prevent lost updates.
In `@test/e2e/integration/frontend-test/all-process-steps.ts`:
- Around line 196-205: The loop over xmlElements currently assigns into
dataModels using dataModels[dataElement.dataType] = ..., which silently
overwrites when multiple elements share the same dataType; change this to
accumulate instead: detect if dataModels[dataElement.dataType] exists and is an
array (create one if not), then push the replaceVariableData(res.body) result
into that array inside the cy.request.then callback so all entries for the same
dataType are preserved; update any downstream consumers to handle arrays for a
given dataType.
In `@test/e2e/support/snapshot.ts`:
- Line 2: The snapshot key generation in snapshot.ts uses only
Cypress.currentTest.titlePath.join(' -- ') (variable key) which causes
collisions across spec files; include the spec identifier (e.g.,
Cypress.spec.name or Cypress.spec.relative) when building the key so it's
spec-unique (for example prepend or append Cypress.spec.name to the existing
titlePath-based key) and update any references that use key accordingly to
preserve behavior while isolating snapshots per spec.
- Around line 9-11: The current branch unconditionally calls
cy.task('writeSnapshot', { key, value: subject }) and logs "New snapshot written
for: \"${key}\"", which auto-creates baselines; change this so snapshot creation
is explicit: detect an "update" flag (e.g. via Cypress.env or a config variable)
and only call writeSnapshot and the cy.log when that flag is true; otherwise do
not write and instead fail the test (throw an Error or use Cypress assertion)
with a clear message that the snapshot for key is missing and must be updated
explicitly. Keep the references to writeSnapshot, the log message "New snapshot
written for", the key variable and subject value so you modify the exact lines.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
yarn.lockis excluded by!**/yarn.lock,!**/*.lock
📒 Files selected for processing (10)
cypress.config.jseslint.config.mjspackage.jsonrenovate.jsonsnapshots.jssnapshots.jsontest/e2e/integration/frontend-test/all-process-steps.tstest/e2e/support/global.tstest/e2e/support/index.tstest/e2e/support/snapshot.ts
💤 Files with no reviewable changes (4)
- eslint.config.mjs
- renovate.json
- package.json
- snapshots.js
# Conflicts: # snapshots.js
|



Description
I spent some time today making sure the
snapshots.jsfile gets updated whenever we upgrade the cypress version (#4035), but then I looked at the dependency dashboard (#410) that the entire plugin is archived and not available anymore. I tried https://github.com/meinaart/cypress-plugin-snapshots instead, but that didn't seem to be compatible with Cypress 10...This re-implements snapshot testing, without any annoying version number in the snapshot file. Plain and simple.
Related Issue(s)
Verification/QA
kind/*andbackport*label to this PR for proper release notes groupingSummary by CodeRabbit
Refactor
New Features
Chores