|
| 1 | +// Flags: --experimental-bench --no-warnings |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +require('../common'); |
| 5 | +const assert = require('assert'); |
| 6 | +const { spawnSync } = require('child_process'); |
| 7 | + |
| 8 | +function runScript(script) { |
| 9 | + return spawnSync(process.execPath, [ |
| 10 | + '--no-warnings', |
| 11 | + '--experimental-bench', |
| 12 | + '-e', |
| 13 | + script, |
| 14 | + ]); |
| 15 | +} |
| 16 | + |
| 17 | +// A failing benchmark consumed through the module-level run() sets the exit |
| 18 | +// code. |
| 19 | +{ |
| 20 | + const child = runScript(` |
| 21 | + const { bench, run } = require('node:bench'); |
| 22 | + bench('failure', () => { throw new Error('boom'); }); |
| 23 | + (async () => { for await (const record of run()); })(); |
| 24 | + `); |
| 25 | + assert.strictEqual(child.status, 1, child.stderr.toString()); |
| 26 | +} |
| 27 | + |
| 28 | +// A successful run through the module-level run() keeps the default exit code. |
| 29 | +{ |
| 30 | + const child = runScript(` |
| 31 | + const { bench, run } = require('node:bench'); |
| 32 | + bench('success', { samples: 1 }, (b) => { |
| 33 | + b.record({ duration_ns: 1n, operations: 1 }); |
| 34 | + }); |
| 35 | + (async () => { for await (const record of run()); })(); |
| 36 | + `); |
| 37 | + assert.strictEqual(child.status, 0, child.stderr.toString()); |
| 38 | +} |
| 39 | + |
| 40 | +// An exit code set by the user is preserved when the explicit run fails. |
| 41 | +{ |
| 42 | + const child = runScript(` |
| 43 | + const { bench, run } = require('node:bench'); |
| 44 | + process.exitCode = 3; |
| 45 | + bench('failure', () => { throw new Error('boom'); }); |
| 46 | + (async () => { for await (const record of run()); })(); |
| 47 | + `); |
| 48 | + assert.strictEqual(child.status, 3, child.stderr.toString()); |
| 49 | +} |
| 50 | + |
| 51 | +// An exit code set by the user is preserved when an automatic run fails. |
| 52 | +{ |
| 53 | + const child = runScript(` |
| 54 | + process.exitCode = 3; |
| 55 | + require('node:bench').bench('failure', () => { throw new Error(); }); |
| 56 | + `); |
| 57 | + assert.strictEqual(child.status, 3, child.stderr.toString()); |
| 58 | +} |
| 59 | + |
| 60 | +// Runners created with createRunner() leave the exit code to the caller. |
| 61 | +{ |
| 62 | + const child = runScript(` |
| 63 | + const runner = require('node:bench').createRunner(); |
| 64 | + runner.bench('failure', () => { throw new Error('boom'); }); |
| 65 | + (async () => { for await (const record of runner.run()); })(); |
| 66 | + `); |
| 67 | + assert.strictEqual(child.status, 0, child.stderr.toString()); |
| 68 | +} |
0 commit comments