Skip to content

.cause of errors innode:test test cases not available to custom reporter #48900

@voxpelli

Description

@voxpelli

Version

18.16.1

Platform

MacOS

Subsystem

test

What steps will reproduce the bug?

I made a Code Sandbox that shows a reproduction.

In short:

import { test } from "node:test";

test("causes", () => {
  const err1 = new Error("Inner");
  throw new Error("Outer", { cause: err1 });
});

And:

```js
export default async function* customReporter(source) {
  for await (const event of source) {
    if (event.type === "test:fail") {
      let err = event.data.details.error;
      if (err.code === "ERR_TEST_FAILURE") {
        err = err.cause;
      }
      yield "Outer error: " + err.message + "\n";
      yield "Inner error: " + err.cause?.message + "\n";
    }
  }
}

Will log:

Outer error: Outer
Inner error: undefined

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

I expected the .cause (and other properties of the Error) to be available to the reporter and thus for the output of the above reproduction to be:

Outer error: Outer
Inner error: Inner

What do you see instead?

Neither the .cause (or other properties I add to the Error) is available to the reporter and the output of the above thus is:

Outer error: Outer
Inner error: undefined

I do see eg the expected property of an AssertionError from Chai or node:assert carry through though, not sure why those are carried through but other properties are not.

Additional information

Getting the full cause chain can be crucial in understanding an unexpected error.

An error chain that's Could not create DB > Unable to connect to DB > <CONNECTION_ERROR> needs all the parts to make sense, but right now node:test reporters are only able to show the topmost error, Could not create DB.

Similar PR in Mocha: mochajs/mocha#4829

Issue regarding adding support for this to my new reporter: voxpelli/node-test-pretty-reporter#2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions