1'use strict';
2
3// This tests snapshot JS API using the example in the docs.
4
5require('../common');
6const assert = require('assert');
7const { spawnSync } = require('child_process');
8const tmpdir = require('../common/tmpdir');
9const path = require('path');
10const fs = require('fs');
11
12tmpdir.refresh();
13const blobPath = path.join(tmpdir.path, 'snapshot.blob');
14{
15  // The list of modules supported in the snapshot is unstable, so just check
16  // a few that are known to work.
17  const code = `
18    require("node:v8");
19    require("node:fs");
20    require("node:fs/promises");
21  `;
22  fs.writeFileSync(
23    path.join(tmpdir.path, 'entry.js'),
24    code,
25    'utf8'
26  );
27  const child = spawnSync(process.execPath, [
28    '--snapshot-blob',
29    blobPath,
30    '--build-snapshot',
31    'entry.js',
32  ], {
33    cwd: tmpdir.path
34  });
35  if (child.status !== 0) {
36    console.log(child.stderr.toString());
37    console.log(child.stdout.toString());
38    assert.strictEqual(child.status, 0);
39  }
40  const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
41  assert(stats.isFile());
42}
43