Skip to content

Commit

Permalink
test: add lib/utils/error-handler.js tests
Browse files Browse the repository at this point in the history
Add unit tests to `lib/utils/error-handler.js`, these are very special
since the module handles some internal state through variables which are
not exposed and binds itself to multiple global `process` events.

Also two minor tweaks/fixes to the original implementation:
- Refactored unused param in `reallyExit()`
- Fixed String.prototype.match group capture usage

PR-URL: #1742
Credit: @ruyadorno
Close: #1742
Reviewed-by: @isaacs
  • Loading branch information
ruyadorno committed Sep 1, 2020
1 parent 758b023 commit 42dd7b8
Show file tree
Hide file tree
Showing 3 changed files with 549 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/utils/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ const exit = (code, noLog) => {
log.verbose('exit', code)
if (log.level === 'silent') noLog = true

const reallyExit = (er) => {
if (er && !code) code = typeof er.errno === 'number' ? er.errno : 1

const reallyExit = () => {
itWorked = !code

// Exit directly -- nothing in the CLI should still be running in the
Expand Down Expand Up @@ -141,9 +139,9 @@ const errorHandler = (er) => {
return exit(1, true)
}

const m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
if (m && !er.code) {
er.code = m
if (!er.code) {
const matchErrorCode = er.message.match(/^(?:Error: )?(E[A-Z]+)/)
er.code = matchErrorCode && matchErrorCode[1]
}

for (const k of ['type', 'stack', 'statusCode', 'pkgid']) {
Expand Down
23 changes: 23 additions & 0 deletions tap-snapshots/test-lib-utils-error-handler.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/lib/utils/error-handler.js TAP handles unknown error > should have expected log contents for unknown error 1`] = `
0 verbose code 1
1 error foo A complete log of this run can be found in:
1 error foo {CWD}/cachefolder/_logs/expecteddate-debug.log
2 verbose stack Error: ERROR
3 verbose cwd {CWD}
4 verbose Foo 1.0.0
5 verbose argv "/node" "{CWD}/test/lib/utils/error-handler.js"
6 verbose node v1.0.0
7 verbose npm v1.0.0
8 error foo code ERROR
9 error foo ERR ERROR
10 error foo ERR ERROR
11 verbose exit 1
`
Loading

0 comments on commit 42dd7b8

Please sign in to comment.