Unverified Commit 12ee9b25 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Defend against json cyles in our execution tests (#81079)

Handle circular references in Jest test results 

Otherwise failing tests may crash the IPC mechanism. e.g. a test failure induced this

```
──── STDERR:             turbopack-tests::execution test_tests__execution__turbopack__basic__esm_this
Input: /Users/lukesandberg/projects/next.js/turbopack/crates/turbopack-tests/tests/execution/turbopack/basic/esm-this
new file turbopack/crates/turbopack-tests/tests/execution/turbopack/basic/esm-this/issues/Error evaluating Node.js code-4c12f2.txt detected:
--- expected
+++ actual
@@ -0,0 +1,6 @@
+error - [transform] [project]/turbopack/crates/turbopack-tests/tests/execution/turbopack/basic/esm-this/input/index.js  Error evaluating Node.js code
+  TypeError: Converting circular structure to JSON
+      --> starting at object with constructor 'Object'
+      --- property 'foo' closes the circle
+      [at <anonymous>]
+      at run (turbopack:///[turbopack-node]/ipc/evaluate.ts:96:52) [WORKSPACE_ROOT/turbopack/crates/turbopack-tests/tests/execution/turbopack/basic/esm-this/output/[turbopack-node]__b000890d._.js:414:66]
\ No newline at end of file
```

With this change we can see the actual test failure

```
thread 'test_tests__execution__turbopack__basic__esm_this' panicked at turbopack/crates/turbopack-tests/tests/execution.rs:106:9:
Failed with error(s) in the following test(s):

"`this` in esm should be undefined":
Error: expect(received).toBe(expected) // Object.is equality

Expected: undefined
Received: {"foo": [Circular]}
    at Object.eval (file:///Users/lukesandberg/projects/next.js/turbopack/crates/turbopack-tests/tests/execution/turbopack/basic/esm-this/output/b1abf_turbopack-tests_tests_execution_turbopack_basic_esm-this_input_index_021e2195.js:18:19)
    at Promise.then.completed (/Users/lukesandberg/projects/next.js/node_modules/.pnpm/jest-circus@29.5.0/node_modules/jest-circus/build/utils.js:293:28)

```
parent 7131d706
...@@ -35,7 +35,23 @@ export default async function run() { ...@@ -35,7 +35,23 @@ export default async function run() {
await import('TESTS') await import('TESTS')
const jestResult = await jest.run() let jestResult = await jest.run()
// Jest test results can contain references to arbitrary objects.
// Defensively remove circular references to avoid breaking our serialization protocol.
const seen = new Set()
jestResult = JSON.parse(
JSON.stringify(jestResult, (k, v) => {
if (v != null && typeof v === 'object') {
if (!seen.has(v)) {
seen.add(v)
return v
} else {
return 'CIRCULAR_REFERENCE_REMOVED'
}
}
return v
})
)
// Wait a full tick for unhandledRejection handlers to run -- a microtask is not sufficient. // Wait a full tick for unhandledRejection handlers to run -- a microtask is not sufficient.
await new Promise((resolve) => setTimeout(resolve, 0)) await new Promise((resolve) => setTimeout(resolve, 0))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment