Sitelet https://github.com/nodejs/node/issues/48900
Skip to content

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

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

Activity

  1. MoLow commented on Jul 24, 2023

    @MoLow
    Member

    this seems to work for me on version 18.17.0 (probably fixed by #47867), can you please check?

  2. MoLow commented on Jul 24, 2023

    @MoLow
    Member

    also, nice work on https://github.com/voxpelli/node-test-pretty-reporter 🎉
    note some of the things there are implemented inside core, e.g #47164

  3. voxpelli commented on Jul 24, 2023

    @voxpelli
    Author

    @MoLow Does indeed look fixed! That's great 👍 Now onto styling them in my reporter

    Thanks for the compliments! #47164 looks nice indeed, I will update my README. I guess diffing of AssertionError:s is not yet supported in core at least?

  4. voxpelli commented on Jul 24, 2023

    @voxpelli
    Author

    In 18.17.0 this started to fail though: https://github.com/voxpelli/node-test-pretty-reporter/blob/cee16190119a1260d96a57d65fb8564a131980b8/lib/errors.js#L10

    For some reason there is no longer a code property on the data.details.error that I'm getting, any idea @MoLow?

  5. MoLow commented on Jul 24, 2023

    @MoLow
    Member

    I guess diffing of AssertionError:s is not yet supported in core at least?

    I am not sure what you mean? AssertError implements an inspect.custom property to diff the assertion

  6. MoLow commented on Jul 24, 2023

    @MoLow
    Member

    For some reason there is no longer a code property on the data.details.error that I'm getting, any idea @MoLow?

    well the example used in this issue does seem to work:

    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";
        }
      }
    }
  7. voxpelli commented on Jul 25, 2023

    @voxpelli
    Author

    I guess diffing of AssertionError:s is not yet supported in core at least?

    I am not sure what you mean? AssertError implements an inspect.custom property to diff the assertion

    Support for the more standard way of doing it, the .expected+ .actual properties on the error: https://mochajs.org/#diffs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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