test(frontend): replace JSON structuredClone polyfill with faithful one#1241
test(frontend): replace JSON structuredClone polyfill with faithful one#1241cristim wants to merge 2 commits into
Conversation
|
@coderabbitai review |
✅ Action performedReview finished.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis change replaces the JSON round-trip polyfill for ChangesstructuredClone polyfill replacement
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Test as deepClone test
participant Global as globalThis.structuredClone
participant Polyfill as structuredClonePolyfill
Test->>Global: structuredClone(value, options)
Global->>Global: check options.transfer
alt transfer provided
Global-->>Test: throw Error
else no transfer
Global->>Polyfill: structuredClonePolyfill(value)
Polyfill-->>Global: cloned value
Global-->>Test: cloned value
end
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
b03f7a5 to
1c8e712
Compare
|
@coderabbitai review |
Rate Limit Exceeded
|
Adversarial review of #1241 found two stale/misleading comments about the new structuredClone polyfill: - utils.test.ts said "via Node's MessageChannel serializer" -- left over from an earlier draft that used MessageChannel; the actual polyfill is @ungap/structured-clone. Update to name the real package. - setup.ts claimed the polyfill "must run inside the jsdom sandbox" so instanceof works. In practice @ungap delegates to Node's native structuredClone when present; instanceof works because jsdom-on-jest shares Date/Map/Set primordials with Node, not because of any sandbox effect. Describe the real mechanism instead. No behavior change.
|
@coderabbitai review |
✅ Action performedReview finished.
|
The Jest setup polyfilled structuredClone for jsdom with a JSON.parse(JSON.stringify(...)) round-trip, which drops undefined-valued properties, stringifies Dates, degrades Map/Set to plain objects, and throws on cycles. Deep-clone tests guarding the deep-copy-state invariant therefore validated weaker semantics than production browsers. Replace the JSON round-trip with @ungap/structured-clone, a faithful implementation of the HTML structured clone algorithm, applied only when the environment does not already provide a native structuredClone. The polyfill runs inside the jsdom sandbox so cloned objects keep sandbox-realm prototypes and instanceof assertions work (a Node-realm clone such as worker_threads MessageChannel round-trip would break them). Unsupported transfer lists now fail loud instead of being silently ignored. Add regression tests asserting real structured clone semantics (undefined preservation, Date/Map/Set fidelity, cyclic references, throw on functions); all five fail against the previous JSON polyfill and pass now. Also update the stale test comment that claimed undefined preservation could not be asserted under jsdom. Closes #1160
Adversarial review of #1241 found two stale/misleading comments about the new structuredClone polyfill: - utils.test.ts said "via Node's MessageChannel serializer" -- left over from an earlier draft that used MessageChannel; the actual polyfill is @ungap/structured-clone. Update to name the real package. - setup.ts claimed the polyfill "must run inside the jsdom sandbox" so instanceof works. In practice @ungap delegates to Node's native structuredClone when present; instanceof works because jsdom-on-jest shares Date/Map/Set primordials with Node, not because of any sandbox effect. Describe the real mechanism instead. No behavior change.
|
Rebased onto origin/main (31 commits ahead). Conflicts resolved in
Gates passed:
New head: df9bd84 |
Problem
TEST-07 (docs/reviews/codebase-review-2026-06-10.md): the Jest setup polyfilled
structuredClonefor jsdom withJSON.parse(JSON.stringify(val)), which dropsundefined-valued properties, stringifiesDates, degradesMap/Setto plain objects, and throws on cycles. Tests guarding the deep-copy-state invariant therefore passed against JSON semantics, not browser semantics, so clone tests could stay green while runtime types drifted in the browser.Fix
frontend/src/__tests__/setup.ts: keep the existing native-first guard (typeof globalThis.structuredClone === 'undefined'), but when polyfilling, delegate to@ungap/structured-clone, a faithful implementation of the HTML structured clone algorithm. It runs inside the jsdom sandbox, so cloned objects keep sandbox-realm prototypes andinstanceofassertions keep working (a Node-realm clone, e.g. aworker_threadsMessageChannel round-trip, deserializes with Node-realm prototypes and breaksinstanceof Date/Map). Unsupportedtransferlists now throw instead of being silently ignored.frontend/src/__tests__/ungap-structured-clone.d.ts: typed module declaration (the package ships no types).frontend/src/__tests__/utils.test.ts: newdeepClone (TEST-07)regression suite asserting real semantics:undefinedpreservation,Dateinstance fidelity,Map/Setfidelity, cyclic references, throw on functions. Also removes the stale comment claimingundefinedpreservation could not be asserted under jsdom.frontend/package.json/package-lock.json: add@ungap/structured-cloneas a devDependency (it was already in the tree as an eslint transitive dep).Test evidence
Tests: 5 failed, 86 skipped, 91 total).Test Suites: 77 passed, 77 total; Tests: 1 skipped, 2558 passed, 2559 total(the skip is pre-existing).npm run typecheckclean;npm run buildsucceeds.Closes #1160
Summary by CodeRabbit
Bug Fixes
undefinedvalues.Tests