Skip to content

test: fix test runner does not print error cause

Rodrigo Muino Tomonari requested to merge github/fork/pulkit-30/fix-44656 into main

fix #44656 (closed)

code:

import test from 'node:test';

test('should print error cause', () => {
  throw new Error('foo', {cause: new Error('bar')});
});

before: test runner doesn't print Error cause

TAP version 13
# Subtest: should print error cause
not ok 1 - should print error cause
  ---
  duration_ms: 0.818167
  failureType: 'testCodeFailure'
  error: 'foo'
  code: 'ERR_TEST_FAILURE'
  stack: |-
    TestContext.<anonymous> (file:///Users/pulkitgupta/Desktop/node/_test.mjs:4:9)
    Test.runInAsyncScope (node:async_hooks:206:9)
    Test.run (node:internal/test_runner/test:549:25)
    Test.start (node:internal/test_runner/test:465:17)
    test (node:internal/test_runner/harness:171:18)
    file:///Users/pulkitgupta/Desktop/node/_test.mjs:3:1
    ModuleJob.run (node:internal/modules/esm/module_job:193:25)
  ...
1..1
# tests 1
# pass 0
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms 5.839667

after changes:

TAP version 13
# Subtest: should print error cause
not ok 1 - should print error cause
  ---
  duration_ms: 0.648959
  failureType: 'testCodeFailure'
  error: 'foo'
  code: 'ERR_TEST_FAILURE'
  stack: |-
    Error: foo
    TestContext.<anonymous> (file:///Users/pulkitgupta/Desktop/node/_test.mjs:4:9)
    Test.runInAsyncScope (node:async_hooks:206:9)
        ... 4 lines matching cause stack trace ...
    ModuleJob.run (node:internal/modules/esm/module_job:193:25) {
      [cause]: Error: bar
          at TestContext.<anonymous> (file:///Users/pulkitgupta/Desktop/node/_test.mjs:4:34)
          at Test.runInAsyncScope (node:async_hooks:206:9)
          at Test.run (node:internal/test_runner/test:549:25)
          at Test.start (node:internal/test_runner/test:465:17)
          at test (node:internal/test_runner/harness:171:18)
          at file:///Users/pulkitgupta/Desktop/node/_test.mjs:3:1
          at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    }
  ...
1..1
# tests 1
# pass 0
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms 5.896167

Merge request reports

Loading