Skip to content

question: best practice for handling unexpected err in tests #13115

Description

@refack
  • Version: master
  • Platform: *
  • Subsystem: test

Ref: #13092 (comment)
Do we have a better construct for handling err in callbacks:
What I found that gives the most informative results (stack of exception and a frame for the test) are:

  • for regular callbacks common.mustCall((err) => {if (err) assert.fail(err)})
  • for .on('error') handlers (err) => assert.fail(err)

That is the only way to get a frame in the stack.

regular callback

common.mustCall((err) => {if (err) assert.fail(err)})

AssertionError [ERR_ASSERTION]: Error: ENOENT: no such file or directory, open 'd:\code\node-cur\test\tmp\watch\foo.txt?'
    at fs.writeFile.common.mustCall (d:\code\node-cur\test\parallel\test-fs-watchfile.js:79:86)
    at d:\code\node-cur\test\common\index.js:504:15
    at fs.js:1239:7
    at FSReqWrap.oncomplete (fs.js:112:15)

with common.mustCall((err) => {assert.ifError(err)})

or common.mustCall(assert.ifError)

assert.js:574
assert.ifError = function ifError(err) { if (err) throw err; };
                                                  ^

Error: ENOENT: no such file or directory, open 'd:\code\node-cur\test\tmp\watch\foo.txt?'

.on('error')

with (err) => assert.fail(err)

assert.js:92
  throw new AssertionError({
  ^

AssertionError [ERR_ASSERTION]: Error: write EPROTO 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:794:

    at ClientRequest.req.on (D:\code\node-cur\test\parallel\test-async-wrap-GH13045.js:63:35)
    at emitOne (events.js:115:13)
    at ClientRequest.emit (events.js:210:7)
    at TLSSocket.socketErrorListener (_http_client.js:397:9)
    ...

with just assert.fail

assert.js:92
  throw new AssertionError({
  ^

AssertionError [ERR_ASSERTION]: Error: write EPROTO 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:794:

    at emitOne (events.js:115:13)
    at ClientRequest.emit (events.js:210:7)
    at TLSSocket.socketErrorListener (_http_client.js:397:9)
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.questionIssues asking questions about Node.js.testIssues and PRs related to Node.js core tests and test infrastructure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions