-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: refactor process/worker exitCode tests
PR-URL: #21739 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
- Loading branch information
1 parent
961f6e8
commit 600349a
Showing
3 changed files
with
133 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
const cases = []; | ||
module.exports = cases; | ||
|
||
function exitsOnExitCodeSet() { | ||
process.exitCode = 42; | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 42); | ||
assert.strictEqual(code, 42); | ||
}); | ||
} | ||
cases.push({ func: exitsOnExitCodeSet, result: 42 }); | ||
|
||
function changesCodeViaExit() { | ||
process.exitCode = 99; | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 42); | ||
assert.strictEqual(code, 42); | ||
}); | ||
process.exit(42); | ||
} | ||
cases.push({ func: changesCodeViaExit, result: 42 }); | ||
|
||
function changesCodeZeroExit() { | ||
process.exitCode = 99; | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 0); | ||
assert.strictEqual(code, 0); | ||
}); | ||
process.exit(0); | ||
} | ||
cases.push({ func: changesCodeZeroExit, result: 0 }); | ||
|
||
function exitWithOneOnUncaught() { | ||
process.exitCode = 99; | ||
process.on('exit', (code) => { | ||
// cannot use assert because it will be uncaughtException -> 1 exit code | ||
// that will render this test useless | ||
if (code !== 1 || process.exitCode !== 1) { | ||
console.log('wrong code! expected 1 for uncaughtException'); | ||
process.exit(99); | ||
} | ||
}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ | ||
func: exitWithOneOnUncaught, | ||
result: 1, | ||
error: /^Error: ok$/, | ||
}); | ||
|
||
function changeCodeInsideExit() { | ||
process.exitCode = 95; | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 95); | ||
assert.strictEqual(code, 95); | ||
process.exitCode = 99; | ||
}); | ||
} | ||
cases.push({ func: changeCodeInsideExit, result: 99 }); | ||
|
||
function zeroExitWithUncaughtHandler() { | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 0); | ||
assert.strictEqual(code, 0); | ||
}); | ||
process.on('uncaughtException', () => {}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ func: zeroExitWithUncaughtHandler, result: 0 }); | ||
|
||
function changeCodeInUncaughtHandler() { | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 97); | ||
assert.strictEqual(code, 97); | ||
}); | ||
process.on('uncaughtException', () => { | ||
process.exitCode = 97; | ||
}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ func: changeCodeInUncaughtHandler, result: 97 }); | ||
|
||
function changeCodeInExitWithUncaught() { | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 1); | ||
assert.strictEqual(code, 1); | ||
process.exitCode = 98; | ||
}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ | ||
func: changeCodeInExitWithUncaught, | ||
result: 98, | ||
error: /^Error: ok$/, | ||
}); | ||
|
||
function exitWithZeroInExitWithUncaught() { | ||
process.on('exit', (code) => { | ||
assert.strictEqual(process.exitCode, 1); | ||
assert.strictEqual(code, 1); | ||
process.exitCode = 0; | ||
}); | ||
throw new Error('ok'); | ||
} | ||
cases.push({ | ||
func: exitWithZeroInExitWithUncaught, | ||
result: 0, | ||
error: /^Error: ok$/, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.