node/test/parallel/test-snapshot-reproducible.js
Joyee Cheung a96256524c
src: dump snapshot source with node:generate_default_snapshot_source
This helps diffing snapshots when the reproducibility gets broken.

PR-URL: https://github.com/nodejs/node/pull/61101
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2026-02-24 00:37:13 +01:00

49 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const assert = require('assert');
common.skipIf32Bits();
// When the test fails this helper can be modified to write outputs
// differently and aid debugging.
function log(line) {
console.log(line);
}
function generateSnapshot() {
tmpdir.refresh();
spawnSyncAndAssert(
process.execPath,
[
'--random_seed=42',
'--predictable',
'--build-snapshot',
'node:generate_default_snapshot_source',
],
{
env: { ...process.env, NODE_DEBUG_NATIVE: 'SNAPSHOT_SERDES' },
cwd: tmpdir.path
},
{
stderr(output) {
const lines = output.split('\n');
for (const line of lines) {
if (line.startsWith('0x')) {
log(line);
}
}
},
}
);
const outputPath = tmpdir.resolve('snapshot.cc');
return fs.readFileSync(outputPath, 'utf-8').split('\n');
}
const source1 = generateSnapshot();
const source2 = generateSnapshot();
assert.deepStrictEqual(source1, source2);