Skip to content

Commit

Permalink
test_runner: serialize 'expected' and 'actual' in isolation
Browse files Browse the repository at this point in the history
Previously, a value seen in 'actual' would be serialized as a circular
reference if it had also appeared in 'expected'.

PR-URL: #51851
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
  • Loading branch information
malthe authored and marco-ippolito committed Feb 29, 2024
1 parent cb62186 commit e4d4bc6
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 39 deletions.
7 changes: 5 additions & 2 deletions lib/internal/test_runner/reporter/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ function jsToYaml(indent, name, value, seen) {
}

if (errIsAssertion) {
result += jsToYaml(indent, 'expected', errExpected, seen);
result += jsToYaml(indent, 'actual', errActual, seen);
// Note that we're deliberately creating shallow copies of the `seen`
// set here in order to isolate the discovery of circular references
// within the expected and actual properties respectively.
result += jsToYaml(indent, 'expected', errExpected, new SafeSet(seen));
result += jsToYaml(indent, 'actual', errActual, new SafeSet(seen));
if (errOperator) {
result += jsToYaml(indent, 'operator', errOperator, seen);
}
Expand Down
32 changes: 24 additions & 8 deletions test/fixtures/test-runner/output/junit_reporter.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -422,35 +422,51 @@ Error [ERR_TEST_FAILURE]: bar
}
</failure>
</testcase>
<testcase name="assertion errors display actual and expected properly" time="*" classname="test" failure="Expected values to be loosely deep-equal:{ bar: 1, foo: 1}should loosely deep-equal&lt;ref *1> { bar: 2, c: [Circular *1]}">
<failure type="testCodeFailure" message="Expected values to be loosely deep-equal:{ bar: 1, foo: 1}should loosely deep-equal&lt;ref *1> { bar: 2, c: [Circular *1]}">
<testcase name="assertion errors display actual and expected properly" time="*" classname="test" failure="Expected values to be loosely deep-equal:{ bar: 1, boo: [ 1 ], foo: 1}should loosely deep-equal{ boo: [ 1 ], circular: &lt;ref *1> { bar: 2, c: [Circular *1] }}">
<failure type="testCodeFailure" message="Expected values to be loosely deep-equal:{ bar: 1, boo: [ 1 ], foo: 1}should loosely deep-equal{ boo: [ 1 ], circular: &lt;ref *1> { bar: 2, c: [Circular *1] }}">
[Error [ERR_TEST_FAILURE]: Expected values to be loosely deep-equal:

{
bar: 1,
boo: [
1
],
foo: 1
}

should loosely deep-equal

&lt;ref *1> {
bar: 2,
c: [Circular *1]
{
boo: [
1
],
circular: &lt;ref *1> {
bar: 2,
c: [Circular *1]
}
}] {
code: 'ERR_TEST_FAILURE',
failureType: 'testCodeFailure',
cause: AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal:

{
bar: 1,
boo: [
1
],
foo: 1
}

should loosely deep-equal

&lt;ref *1> {
bar: 2,
c: [Circular *1]
{
boo: [
1
],
circular: &lt;ref *1> {
bar: 2,
c: [Circular *1]
}
}
* {
generatedMessage: true,
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/test-runner/output/lcov_reporter.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ DA:388,1
DA:389,1
DA:390,1
DA:391,1
LH:389
LF:391
DA:392,1
LH:390
LF:392
end_of_record
3 changes: 2 additions & 1 deletion test/fixtures/test-runner/output/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,9 @@ test('assertion errors display actual and expected properly', async () => {
circular.c = circular;
const tmpLimit = Error.stackTraceLimit;
Error.stackTraceLimit = 1;
const boo = [1];
try {
assert.deepEqual({ foo: 1, bar: 1 }, circular); // eslint-disable-line no-restricted-properties
assert.deepEqual({ foo: 1, bar: 1, boo }, { boo, circular }); // eslint-disable-line no-restricted-properties
} catch (err) {
Error.stackTraceLimit = tmpLimit;
throw err;
Expand Down
23 changes: 18 additions & 5 deletions test/fixtures/test-runner/output/output.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -643,23 +643,36 @@ not ok 61 - assertion errors display actual and expected properly

{
bar: 1,
boo: [
1
],
foo: 1
}

should loosely deep-equal

<ref *1> {
bar: 2,
c: [Circular *1]
{
boo: [
1
],
circular: <ref *1> {
bar: 2,
c: [Circular *1]
}
}
code: 'ERR_ASSERTION'
name: 'AssertionError'
expected:
bar: 2
c: <Circular>
boo:
0: 1
circular:
bar: 2
c: <Circular>
actual:
foo: 1
bar: 1
boo:
0: 1
operator: 'deepEqual'
stack: |-
*
Expand Down
23 changes: 18 additions & 5 deletions test/fixtures/test-runner/output/output_cli.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -643,23 +643,36 @@ not ok 61 - assertion errors display actual and expected properly

{
bar: 1,
boo: [
1
],
foo: 1
}

should loosely deep-equal

<ref *1> {
bar: 2,
c: [Circular *1]
{
boo: [
1
],
circular: <ref *1> {
bar: 2,
c: [Circular *1]
}
}
code: 'ERR_ASSERTION'
name: 'AssertionError'
expected:
bar: 2
c: <Circular>
boo:
0: 1
circular:
bar: 2
c: <Circular>
actual:
foo: 1
bar: 1
boo:
0: 1
operator: 'deepEqual'
stack: |-
*
Expand Down
28 changes: 22 additions & 6 deletions test/fixtures/test-runner/output/spec_reporter.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,22 @@

{
bar: 1,
boo: [
1
],
foo: 1
}

should loosely deep-equal

<ref *1> {
bar: 2,
c: [Circular *1]
{
boo: [
1
],
circular: <ref *1> {
bar: 2,
c: [Circular *1]
}
}
* {
generatedMessage: true,
Expand Down Expand Up @@ -528,14 +536,22 @@

{
bar: 1,
boo: [
1
],
foo: 1
}

should loosely deep-equal

<ref *1> {
bar: 2,
c: [Circular *1]
{
boo: [
1
],
circular: <ref *1> {
bar: 2,
c: [Circular *1]
}
}
* {
generatedMessage: true,
Expand Down
36 changes: 26 additions & 10 deletions test/fixtures/test-runner/output/spec_reporter_cli.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -263,20 +263,28 @@

{
bar: 1,
boo: [
1
],
foo: 1
}

should loosely deep-equal

<ref *1> {
bar: 2,
c: [Circular *1]
{
boo: [
1
],
circular: <ref *1> {
bar: 2,
c: [Circular *1]
}
}
* {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: { foo: 1, bar: 1 },
expected: <ref *1> { bar: 2, c: [Circular *1] },
actual: { foo: 1, bar: 1, boo: [ 1 ] },
expected: { boo: [ 1 ], circular: <ref *1> { bar: 2, c: [Circular *1] } },
operator: 'deepEqual'
}

Expand Down Expand Up @@ -528,20 +536,28 @@

{
bar: 1,
boo: [
1
],
foo: 1
}

should loosely deep-equal

<ref *1> {
bar: 2,
c: [Circular *1]
{
boo: [
1
],
circular: <ref *1> {
bar: 2,
c: [Circular *1]
}
}
* {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: { foo: 1, bar: 1 },
expected: <ref *1> { bar: 2, c: [Circular *1] },
actual: { foo: 1, bar: 1, boo: [ 1 ] },
expected: { boo: [ 1 ], circular: <ref *1> { bar: 2, c: [Circular *1] } },
operator: 'deepEqual'
}

Expand Down

0 comments on commit e4d4bc6

Please sign in to comment.