1'use strict'; 2 3const assert = require('assert'); 4 5const cases = []; 6module.exports = cases; 7 8function exitsOnExitCodeSet() { 9 process.exitCode = 42; 10 process.on('exit', (code) => { 11 assert.strictEqual(process.exitCode, 42); 12 assert.strictEqual(code, 42); 13 }); 14} 15cases.push({ func: exitsOnExitCodeSet, result: 42 }); 16 17function changesCodeViaExit() { 18 process.exitCode = 99; 19 process.on('exit', (code) => { 20 assert.strictEqual(process.exitCode, 42); 21 assert.strictEqual(code, 42); 22 }); 23 process.exit(42); 24} 25cases.push({ func: changesCodeViaExit, result: 42 }); 26 27function changesCodeZeroExit() { 28 process.exitCode = 99; 29 process.on('exit', (code) => { 30 assert.strictEqual(process.exitCode, 0); 31 assert.strictEqual(code, 0); 32 }); 33 process.exit(0); 34} 35cases.push({ func: changesCodeZeroExit, result: 0 }); 36 37function exitWithOneOnUncaught() { 38 process.exitCode = 99; 39 process.on('exit', (code) => { 40 // cannot use assert because it will be uncaughtException -> 1 exit code 41 // that will render this test useless 42 if (code !== 1 || process.exitCode !== 1) { 43 console.log('wrong code! expected 1 for uncaughtException'); 44 process.exit(99); 45 } 46 }); 47 throw new Error('ok'); 48} 49cases.push({ 50 func: exitWithOneOnUncaught, 51 result: 1, 52 error: /^Error: ok$/, 53}); 54 55function changeCodeInsideExit() { 56 process.exitCode = 95; 57 process.on('exit', (code) => { 58 assert.strictEqual(process.exitCode, 95); 59 assert.strictEqual(code, 95); 60 process.exitCode = 99; 61 }); 62} 63cases.push({ func: changeCodeInsideExit, result: 99 }); 64 65function zeroExitWithUncaughtHandler() { 66 process.on('exit', (code) => { 67 assert.strictEqual(process.exitCode, 0); 68 assert.strictEqual(code, 0); 69 }); 70 process.on('uncaughtException', () => {}); 71 throw new Error('ok'); 72} 73cases.push({ func: zeroExitWithUncaughtHandler, result: 0 }); 74 75function changeCodeInUncaughtHandler() { 76 process.on('exit', (code) => { 77 assert.strictEqual(process.exitCode, 97); 78 assert.strictEqual(code, 97); 79 }); 80 process.on('uncaughtException', () => { 81 process.exitCode = 97; 82 }); 83 throw new Error('ok'); 84} 85cases.push({ func: changeCodeInUncaughtHandler, result: 97 }); 86 87function changeCodeInExitWithUncaught() { 88 process.on('exit', (code) => { 89 assert.strictEqual(process.exitCode, 1); 90 assert.strictEqual(code, 1); 91 process.exitCode = 98; 92 }); 93 throw new Error('ok'); 94} 95cases.push({ 96 func: changeCodeInExitWithUncaught, 97 result: 98, 98 error: /^Error: ok$/, 99}); 100 101function exitWithZeroInExitWithUncaught() { 102 process.on('exit', (code) => { 103 assert.strictEqual(process.exitCode, 1); 104 assert.strictEqual(code, 1); 105 process.exitCode = 0; 106 }); 107 throw new Error('ok'); 108} 109cases.push({ 110 func: exitWithZeroInExitWithUncaught, 111 result: 0, 112 error: /^Error: ok$/, 113}); 114