Files
scriptc/tests/harness/node-test-normalize.ts
T
Chris Tate 89b5baedd7 Add t.assert.snapshot and t.assert.fileSnapshot
node:test changes, matching Node 24.22:

- t.assert.snapshot(value[, options]) serializes the value with Node's
  default serializer, JSON.stringify(value, null, 2), or with an
  options.serializers array literal applied in order, and compares it
  with assert.strictEqual against the snapshot stored under
  "<test full name> <n>" in "<entry file>.snapshot".
- t.assert.fileSnapshot(value, path[, options]) compares the serialized
  value with the contents of path.
- Snapshot files are read in the form Node writes them, exports[`key`] =
  `value`; statements, with template escapes and CRLF line endings
  cooked as in a template literal.
- Missing snapshots, unreadable snapshot files and throwing serializers
  throw Node's ERR_INVALID_STATE errors, with Node's messages.
- Running the binary with Node's --test-update-snapshots flag as its
  first argument records the values and writes the snapshot files after
  the last test, keys sorted as Node sorts them. The flag is kept out of
  process.argv and appears in process.execArgv, which is otherwise [].
- Snapshot assertions count toward the test's plan.

The snapshot fixture compares the reporter output with Node, plain and
with SCRIPTC_SAN=1, against a snapshot file Node wrote. The update-mode
harness test runs Node and the binary with --test-update-snapshots and
compares the files they write. The node:test normalization also drops
the "... N lines matching cause stack trace ..." line Node prints for an
error cause, with the stack frames it summarizes.
2026-10-18 05:31:40 -05:00

33 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* The node:test differential's documented output normalization —
* node-test.test.ts's function, extracted so the Linux lane applies the
* IDENTICAL scrub to both of its in-container lanes. Node's spec reporter
* embeds real durations in every line (raw byte-compare is impossible for
* ANY node:test program); stack frames, the "... N lines matching cause
* stack trace ..." line inspect folds an error cause's shared frames into,
* and the inspect property block a frame opens are runner-internal output
* the compiled runtime deliberately omits (SEMANTICS.md). Everything else
* must match byte-exactly. */
/** The documented normalization — durations, frames, the property block. */
export function normalizeNodeTestOutput(stdout: string): string {
const out: string[] = [];
let inPropBlock = false;
for (const line of stdout.split("\n")) {
if (inPropBlock) {
if (/^\s*\}\s*$/.test(line)) inPropBlock = false;
continue;
}
if (/^\s+\.\.\. \d+ lines? matching cause stack trace \.\.\.$/.test(line)) continue;
if (/^\s+at /.test(line)) {
if (line.trimEnd().endsWith("{")) inPropBlock = true;
continue;
}
out.push(
line
.replace(/\(\d+(?:\.\d+)?ms\)/g, "(Xms)")
.replace(/^ℹ duration_ms .*$/, "ℹ duration_ms X"),
);
}
return out.join("\n");
}