Repository navigation
Description
Version
18.17.0
Platform
MacOS
Subsystem
test
What steps will reproduce the bug?
In a test file like:
import { test } from "node:test";
test("failing test", () => {
throw new Error('The failure');
});Run that with node --test and you'll get:
✖ failing test (0.59475ms)
Error: The failure
at TestContext.<anonymous>
Add in chai.should():
import { test } from "node:test";
import chai from 'chai';
chai.should();
test("failing test", () => {
throw new Error('The failure');
});And you instead get:
[Error [ERR_TEST_FAILURE]: The failure] {
[cause]: Error: The failure
at TestContext.<anonymous>
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
All ERR_TEST_FAILURE events given in the data.details.error of a TestStream test:fail should have an err.code and should have that equal 'ERR_TEST_FAILURE', signaling that its err.cause contains the error thrown in the failing test:
| const err = error.code === 'ERR_TEST_FAILURE' ? error.cause : error; |
What do you see instead?
In Node 18.16.1 I saw that, but in Node 18.17.0 adding chai.should() makes it so that no err.code at all appears in the errors in data.details.error in the 'test:fail' TestStream event, yet the stack trace shows that its indeed a ERR_TEST_FAILURE in there somewhere.
This causes checks like these to fail:
| const err = error.code === 'ERR_TEST_FAILURE' ? error.cause : error; |
And eg. causes my diff of Chai errors in @voxpelli/node-test-pretty-reporter to no longer work (unless I do workarounds and check for the presence of the 'ERR_TEST_FAILURE' string in the stack).
Additional information
This is a follow up to #48900 (comment) and is probably caused by the #47867 that @MoLow mentioned in there and which fixed that issue.
Activity
MoLow commentedon Jul 25, 2023
can you alaborate more?
I run this piece of code, which does nothing, and throws no error:
also - in your example you call this outside of a test - is that intended?
voxpelli commentedon Jul 25, 2023
@MoLow The mere act of
chai.should()makes it so that noerr.codeis found onERR_TEST_FAILUREerrors in the reporters, including the built in one.This is the minimal reproduction of the issue I saw here but you couldn’t reproduce: #48900 (comment)
After narrowing it down I realized that the mere activation of the “should”-style of Chai caused it (the one that enables one to write
foo.should.equal('bar'))cjihrig commentedon Jul 25, 2023
This appears to be related to Node's error serialization, and not specific to the test runner. It might be related to 78972d4696, which is new in 18.17.0, but I have not bisected.
The bug shows up with
--test, but not if the file is run directly. When--testis used and the test fails, it serializes the error (viaserializeError()) in order to report it.serializeError()callsTryGetAllProperties(), which iterates over everything on the error's prototype.chai.should()creates anObject.prototype.shouldgetter. Node's error serialization triggers that getter inTryGetAllProperties(). That getter introduces aProxyon theObjectprototype, which breaks the serialization.Here is a more minimal repro (note you must start Node with
--expose-internals):I would say that chai should not be doing what it's doing, but Node could also handle it better.
[-]`18.17.0` test runner gives reporters no error metadata with `chai.should()`[/-][+]`chai.should()` breaks error serialization[/+]BridgeAR commentedon Sep 12, 2025
I am going to close this since the prototype of Object is manipulated and that is not a good idea. It could have side effects everywhere.