diff --git a/lib/internal/v8/startup_snapshot.js b/lib/internal/v8/startup_snapshot.js index 5d88748b1c4719..49623627706bc0 100644 --- a/lib/internal/v8/startup_snapshot.js +++ b/lib/internal/v8/startup_snapshot.js @@ -4,8 +4,10 @@ const { validateFunction, } = require('internal/validators'); const { - ERR_NOT_BUILDING_SNAPSHOT, - ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION, + codes: { + ERR_NOT_BUILDING_SNAPSHOT, + ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION, + }, } = require('internal/errors'); const { diff --git a/test/fixtures/snapshot/v8-startup-snapshot-api.js b/test/fixtures/snapshot/v8-startup-snapshot-api.js index f41f519755f45e..9eccc655990572 100644 --- a/test/fixtures/snapshot/v8-startup-snapshot-api.js +++ b/test/fixtures/snapshot/v8-startup-snapshot-api.js @@ -30,3 +30,8 @@ addDeserializeCallback(({ filePath }) => { setDeserializeMainFunction(({ filePath }) => { console.log(storage[filePath].toString()); }, { filePath }); +assert.throws(() => setDeserializeMainFunction(() => { + assert.fail('unreachable duplicated main function'); +}), { + code: 'ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION', +}); diff --git a/test/parallel/test-v8-startup-snapshot-api.js b/test/parallel/test-v8-startup-snapshot-api.js new file mode 100644 index 00000000000000..c3738911540fc1 --- /dev/null +++ b/test/parallel/test-v8-startup-snapshot-api.js @@ -0,0 +1,26 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +const { + isBuildingSnapshot, + addSerializeCallback, + addDeserializeCallback, + setDeserializeMainFunction +} = require('v8').startupSnapshot; + +// This test verifies that the v8.startupSnapshot APIs are not available when +// it is not building snapshot. + +assert(!isBuildingSnapshot()); + +assert.throws(() => addSerializeCallback(() => {}), { + code: 'ERR_NOT_BUILDING_SNAPSHOT', +}); +assert.throws(() => addDeserializeCallback(() => {}), { + code: 'ERR_NOT_BUILDING_SNAPSHOT', +}); +assert.throws(() => setDeserializeMainFunction(() => {}), { + code: 'ERR_NOT_BUILDING_SNAPSHOT', +});