1'use strict'; 2 3// This tests that the errors in the snapshot script can be handled 4// properly. 5 6require('../common'); 7const assert = require('assert'); 8const { spawnSync } = require('child_process'); 9const tmpdir = require('../common/tmpdir'); 10const fixtures = require('../common/fixtures'); 11const path = require('path'); 12const fs = require('fs'); 13 14tmpdir.refresh(); 15const blobPath = path.join(tmpdir.path, 'snapshot.blob'); 16const entry = fixtures.path('snapshot', 'error.js'); 17 18// --build-snapshot should be run with an entry point. 19{ 20 const child = spawnSync(process.execPath, [ 21 '--snapshot-blob', 22 blobPath, 23 '--build-snapshot', 24 ], { 25 cwd: tmpdir.path 26 }); 27 const stderr = child.stderr.toString(); 28 console.log(child.status); 29 console.log(stderr); 30 console.log(child.stdout.toString()); 31 assert.strictEqual(child.status, 9); 32 assert.match(stderr, 33 /--build-snapshot must be used with an entry point script/); 34 assert(!fs.existsSync(path.join(tmpdir.path, 'snapshot.blob'))); 35} 36 37// Loading a non-existent snapshot should fail. 38{ 39 const child = spawnSync(process.execPath, [ 40 '--snapshot-blob', 41 blobPath, 42 entry, 43 ], { 44 cwd: tmpdir.path 45 }); 46 const stderr = child.stderr.toString(); 47 console.log(child.status); 48 console.log(stderr); 49 console.log(child.stdout.toString()); 50 assert.strictEqual(child.status, 1); 51 assert.match(stderr, /Cannot open/); 52 assert(!fs.existsSync(path.join(tmpdir.path, 'snapshot.blob'))); 53} 54 55 56// Running an script that throws an error should result in an exit code of 1. 57{ 58 const child = spawnSync(process.execPath, [ 59 '--snapshot-blob', 60 blobPath, 61 '--build-snapshot', 62 entry, 63 ], { 64 cwd: tmpdir.path 65 }); 66 const stderr = child.stderr.toString(); 67 console.log(child.status); 68 console.log(stderr); 69 console.log(child.stdout.toString()); 70 assert.strictEqual(child.status, 1); 71 assert.match(stderr, /error\.js:1/); 72 assert(!fs.existsSync(path.join(tmpdir.path, 'snapshot.blob'))); 73} 74