Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
voxpelli opened this issue Jul 24, 2023 · 7 comments
Closed

Comments

@voxpelli
Copy link

voxpelli commented Jul 24, 2023

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

@MoLow
Copy link
Member

MoLow commented Jul 24, 2023

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

@MoLow
Copy link
Member

MoLow commented Jul 24, 2023

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

@voxpelli
Copy link
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?

@voxpelli voxpelli closed this as not planned Won't fix, can't repro, duplicate, stale Jul 24, 2023
@voxpelli
Copy link
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?

@MoLow
Copy link
Member

MoLow commented Jul 24, 2023

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

@MoLow
Copy link
Member

MoLow commented Jul 24, 2023

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";
    }
  }
}

@voxpelli
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants