Skip to content

test_runner: incorrect unwrapping of errors in after() when using the tap reporter #48941

Closed
@mcollina

Description

@mcollina

Consider this test:

const test = require('node:test')

test('should print the error', (t) => {
  t.after(async () => {
    throw new Error('kaboom')
  })
})

If I run it normally, I get:

✖ should print the error (0.880917ms)
  Error: kaboom
      at TestContext.<anonymous> (/Users/matteo/tmp/bug.js:5:11)
      at TestHook.runInAsyncScope (node:async_hooks:206:9)
      at TestHook.run (node:internal/test_runner/test:580:25)
      at TestHook.run (node:internal/test_runner/test:759:18)
      at TestHook.run (node:internal/util:500:12)
      at node:internal/test_runner/test:516:20
      at async Test.runHook (node:internal/test_runner/test:514:7)
      at async after (node:internal/test_runner/test:542:9)
      at async Test.run (node:internal/test_runner/test:590:7)
      at async startSubtest (node:internal/test_runner/harness:204:3)

ℹ tests 1
ℹ suites 0
ℹ pass 0
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 40.280833

✖ failing tests:

✖ should print the error (0.880917ms)
  Error: kaboom
      at TestContext.<anonymous> (/Users/matteo/tmp/bug.js:5:11)
      at TestHook.runInAsyncScope (node:async_hooks:206:9)
      at TestHook.run (node:internal/test_runner/test:580:25)
      at TestHook.run (node:internal/test_runner/test:759:18)
      at TestHook.run (node:internal/util:500:12)
      at node:internal/test_runner/test:516:20
      at async Test.runHook (node:internal/test_runner/test:514:7)
      at async after (node:internal/test_runner/test:542:9)
      at async Test.run (node:internal/test_runner/test:590:7)
      at async startSubtest (node:internal/test_runner/harness:204:3)

If I run in a non-interactive process such as node --test bug.js | less, I get the error detail scrambled:

TAP version 13
# Subtest: should print the error
not ok 1 - should print the error
  ---
  duration_ms: 0.974875
  failureType: 'hookFailed'
  error: 'failed running after hook'
  code: 'ERR_TEST_FAILURE'
  stack: |-
    TestContext.<anonymous> (/Users/matteo/tmp/bug.js:5:11)
    TestHook.runInAsyncScope (node:async_hooks:206:9)
    TestHook.run (node:internal/test_runner/test:580:25)
    TestHook.run (node:internal/test_runner/test:759:18)
    TestHook.run (node:internal/util:500:12)
    node:internal/test_runner/test:516:20
    async Test.runHook (node:internal/test_runner/test:514:7)
    async after (node:internal/test_runner/test:542:9)
    async Test.run (node:internal/test_runner/test:590:7)
    async startSubtest (node:internal/test_runner/harness:204:3)
  ...
1..1
# tests 1
# suites 0
# pass 0
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms 43.341875

This shows specifically during CI runs, where the pretty reporter is not engaged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions