mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
test_runner: stringify AssertError expected and actual
PR-URL: https://github.com/nodejs/node/pull/47088 Fixes: https://github.com/nodejs/node/issues/47075 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
1 parent
2cea7d8141
commit
eb837ce80d
7 files changed
+173
-29
No files matched your search
@@ -6,6 +6,7 @@ const {
|
||||
ObjectEntries,
|
||||
RegExpPrototypeSymbolReplace,
|
||||
SafeMap,
|
||||
SafeSet,
|
||||
StringPrototypeReplaceAll,
|
||||
StringPrototypeSplit,
|
||||
StringPrototypeRepeat,
|
||||
@@ -79,7 +80,7 @@ function reportDetails(nesting, data = kEmptyObject) {
|
||||
|
||||
details += jsToYaml(_indent, 'duration_ms', duration_ms);
|
||||
details += jsToYaml(_indent, 'type', data.type);
|
||||
details += jsToYaml(_indent, null, error);
|
||||
details += jsToYaml(_indent, null, error, new SafeSet());
|
||||
details += `${_indent} ...\n`;
|
||||
return details;
|
||||
}
|
||||
@@ -109,7 +110,7 @@ function tapEscape(input) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function jsToYaml(indent, name, value) {
|
||||
function jsToYaml(indent, name, value, seen) {
|
||||
if (value === null || value === undefined) {
|
||||
return '';
|
||||
}
|
||||
@@ -136,9 +137,16 @@ function jsToYaml(indent, name, value) {
|
||||
return str;
|
||||
}
|
||||
|
||||
seen.add(value);
|
||||
const entries = ObjectEntries(value);
|
||||
const isErrorObj = isError(value);
|
||||
let result = '';
|
||||
let propsIndent = indent;
|
||||
|
||||
if (name != null) {
|
||||
result += `${indent} ${name}:\n`;
|
||||
propsIndent += ' ';
|
||||
}
|
||||
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
const { 0: key, 1: value } = entries[i];
|
||||
@@ -146,8 +154,12 @@ function jsToYaml(indent, name, value) {
|
||||
if (isErrorObj && (key === 'cause' || key === 'code')) {
|
||||
continue;
|
||||
}
|
||||
if (seen.has(value)) {
|
||||
result += `${propsIndent} ${key}: <Circular>\n`;
|
||||
continue;
|
||||
}
|
||||
|
||||
result += jsToYaml(indent, key, value);
|
||||
result += jsToYaml(propsIndent, key, value, seen);
|
||||
}
|
||||
|
||||
if (isErrorObj) {
|
||||
@@ -189,20 +201,20 @@ function jsToYaml(indent, name, value) {
|
||||
}
|
||||
}
|
||||
|
||||
result += jsToYaml(indent, 'error', errMsg);
|
||||
result += jsToYaml(indent, 'error', errMsg, seen);
|
||||
|
||||
if (errCode) {
|
||||
result += jsToYaml(indent, 'code', errCode);
|
||||
result += jsToYaml(indent, 'code', errCode, seen);
|
||||
}
|
||||
if (errName && errName !== 'Error') {
|
||||
result += jsToYaml(indent, 'name', errName);
|
||||
result += jsToYaml(indent, 'name', errName, seen);
|
||||
}
|
||||
|
||||
if (errIsAssertion) {
|
||||
result += jsToYaml(indent, 'expected', errExpected);
|
||||
result += jsToYaml(indent, 'actual', errActual);
|
||||
result += jsToYaml(indent, 'expected', errExpected, seen);
|
||||
result += jsToYaml(indent, 'actual', errActual, seen);
|
||||
if (errOperator) {
|
||||
result += jsToYaml(indent, 'operator', errOperator);
|
||||
result += jsToYaml(indent, 'operator', errOperator, seen);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ const {
|
||||
StringPrototypeSubstring,
|
||||
} = primordials;
|
||||
|
||||
const kYamlKeyRegex = /^(\s+)?(\w+):(\s)+([>|][-+])?(.*)$/;
|
||||
const kYamlKeyRegex = /^(\s+)?(\w+):(\s)*([>|][-+])?(.*)$/;
|
||||
const kStackDelimiter = ' at ';
|
||||
|
||||
function reConstructError(parsedYaml) {
|
||||
@@ -91,28 +91,39 @@ function YAMLToJs(lines) {
|
||||
return undefined;
|
||||
}
|
||||
const result = { __proto__: null };
|
||||
let context = { __proto__: null, object: result, indent: 0, currentKey: null };
|
||||
let isInYamlBlock = false;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (isInYamlBlock && !StringPrototypeStartsWith(line, StringPrototypeRepeat(' ', isInYamlBlock.indent))) {
|
||||
result[isInYamlBlock.key] = isInYamlBlock.key === 'stack' ?
|
||||
result[isInYamlBlock.key] : ArrayPrototypeJoin(result[isInYamlBlock.key], '\n');
|
||||
context.object[isInYamlBlock.key] = isInYamlBlock.key === 'stack' ?
|
||||
context.object[isInYamlBlock.key] : ArrayPrototypeJoin(context.object[isInYamlBlock.key], '\n');
|
||||
isInYamlBlock = false;
|
||||
}
|
||||
if (isInYamlBlock) {
|
||||
const blockLine = StringPrototypeSubstring(line, isInYamlBlock.indent);
|
||||
ArrayPrototypePush(result[isInYamlBlock.key], blockLine);
|
||||
ArrayPrototypePush(context.object[isInYamlBlock.key], blockLine);
|
||||
continue;
|
||||
}
|
||||
const match = RegExpPrototypeExec(kYamlKeyRegex, line);
|
||||
if (match !== null) {
|
||||
const { 1: leadingSpaces, 2: key, 4: block, 5: value } = match;
|
||||
const indent = leadingSpaces?.length ?? 0;
|
||||
if (block) {
|
||||
isInYamlBlock = { key, indent: (leadingSpaces?.length ?? 0) + 2 };
|
||||
result[key] = [];
|
||||
} else {
|
||||
result[key] = getYamlValue(value);
|
||||
isInYamlBlock = { key, indent: indent + 2 };
|
||||
context.object[key] = [];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (indent > context.indent) {
|
||||
context.object[context.currentKey] ||= {};
|
||||
context = { __proto__: null, parent: context, object: context.object[context.currentKey], indent };
|
||||
} else if (indent < context.indent) {
|
||||
context = context.parent;
|
||||
}
|
||||
|
||||
context.currentKey = key;
|
||||
context.object[key] = getYamlValue(value);
|
||||
}
|
||||
}
|
||||
return reConstructError(result);
|
||||
|
||||
@@ -389,3 +389,17 @@ test('unfinished test with unhandledRejection', async () => {
|
||||
setImmediate(() => {
|
||||
throw new Error('uncaught from outside of a test');
|
||||
});
|
||||
|
||||
test('assertion errors display actual and expected properly', async () => {
|
||||
// Make sure the assert module is handled.
|
||||
const circular = { bar: 2 };
|
||||
circular.c = circular;
|
||||
const tmpLimit = Error.stackTraceLimit;
|
||||
Error.stackTraceLimit = 1;
|
||||
try {
|
||||
assert.deepEqual({ foo: 1, bar: 1 }, circular); // eslint-disable-line no-restricted-properties
|
||||
} catch (err) {
|
||||
Error.stackTraceLimit = tmpLimit;
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
@@ -624,8 +624,39 @@ not ok 64 - unfinished test with unhandledRejection
|
||||
*
|
||||
*
|
||||
...
|
||||
# Subtest: assertion errors display actual and expected properly
|
||||
not ok 65 - assertion errors display actual and expected properly
|
||||
---
|
||||
duration_ms: *
|
||||
failureType: 'testCodeFailure'
|
||||
error: |-
|
||||
Expected values to be loosely deep-equal:
|
||||
|
||||
{
|
||||
bar: 1,
|
||||
foo: 1
|
||||
}
|
||||
|
||||
should loosely deep-equal
|
||||
|
||||
<ref *1> {
|
||||
bar: 2,
|
||||
c: [Circular *1]
|
||||
}
|
||||
code: 'ERR_ASSERTION'
|
||||
name: 'AssertionError'
|
||||
expected:
|
||||
bar: 2
|
||||
c: <Circular>
|
||||
actual:
|
||||
foo: 1
|
||||
bar: 1
|
||||
operator: 'deepEqual'
|
||||
stack: |-
|
||||
*
|
||||
...
|
||||
# Subtest: invalid subtest fail
|
||||
not ok 65 - invalid subtest fail
|
||||
not ok 66 - invalid subtest fail
|
||||
---
|
||||
duration_ms: *
|
||||
failureType: 'parentAlreadyFinished'
|
||||
@@ -634,7 +665,7 @@ not ok 65 - invalid subtest fail
|
||||
stack: |-
|
||||
*
|
||||
...
|
||||
1..65
|
||||
1..66
|
||||
# Warning: Test "unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
|
||||
# Warning: Test "async unhandled rejection - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from async unhandled rejection fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
|
||||
# Warning: A resource generated asynchronous activity after the test ended. This activity created the error "Error: uncaught from outside of a test" which triggered an uncaughtException event, caught by the test runner.
|
||||
@@ -642,10 +673,10 @@ not ok 65 - invalid subtest fail
|
||||
# Warning: Test "immediate reject - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
|
||||
# Warning: Test "callback called twice in different ticks" generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event.
|
||||
# Warning: Test "callback async throw after done" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from callback async throw after done" and would have caused the test to fail, but instead triggered an uncaughtException event.
|
||||
# tests 79
|
||||
# tests 80
|
||||
# suites 0
|
||||
# pass 37
|
||||
# fail 24
|
||||
# fail 25
|
||||
# cancelled 3
|
||||
# skipped 10
|
||||
# todo 5
|
||||
|
||||
@@ -624,8 +624,39 @@ not ok 64 - unfinished test with unhandledRejection
|
||||
*
|
||||
*
|
||||
...
|
||||
# Subtest: assertion errors display actual and expected properly
|
||||
not ok 65 - assertion errors display actual and expected properly
|
||||
---
|
||||
duration_ms: *
|
||||
failureType: 'testCodeFailure'
|
||||
error: |-
|
||||
Expected values to be loosely deep-equal:
|
||||
|
||||
{
|
||||
bar: 1,
|
||||
foo: 1
|
||||
}
|
||||
|
||||
should loosely deep-equal
|
||||
|
||||
<ref *1> {
|
||||
bar: 2,
|
||||
c: [Circular *1]
|
||||
}
|
||||
code: 'ERR_ASSERTION'
|
||||
name: 'AssertionError'
|
||||
expected:
|
||||
bar: 2
|
||||
c: '<Circular>'
|
||||
actual:
|
||||
foo: 1
|
||||
bar: 1
|
||||
operator: 'deepEqual'
|
||||
stack: |-
|
||||
*
|
||||
...
|
||||
# Subtest: invalid subtest fail
|
||||
not ok 65 - invalid subtest fail
|
||||
not ok 66 - invalid subtest fail
|
||||
---
|
||||
duration_ms: *
|
||||
failureType: 'parentAlreadyFinished'
|
||||
@@ -641,11 +672,11 @@ not ok 65 - invalid subtest fail
|
||||
# Warning: Test "immediate reject - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
|
||||
# Warning: Test "callback called twice in different ticks" generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event.
|
||||
# Warning: Test "callback async throw after done" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from callback async throw after done" and would have caused the test to fail, but instead triggered an uncaughtException event.
|
||||
1..65
|
||||
# tests 79
|
||||
1..66
|
||||
# tests 80
|
||||
# suites 0
|
||||
# pass 37
|
||||
# fail 24
|
||||
# fail 25
|
||||
# cancelled 3
|
||||
# skipped 10
|
||||
# todo 5
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
..XX...X..XXX.X.....
|
||||
XXX.....X..X...X....
|
||||
.........X...XXX.XX.
|
||||
.....XXXXXXX...XXXX
|
||||
.....XXXXXXX...XXXXX
|
||||
|
||||
@@ -184,7 +184,7 @@
|
||||
callback called twice in different ticks (*ms)
|
||||
callback called twice in future tick (*ms)
|
||||
Error [ERR_TEST_FAILURE]: callback invoked multiple times
|
||||
*
|
||||
* {
|
||||
failureType: 'multipleCallbackInvocations',
|
||||
cause: 'callback invoked multiple times',
|
||||
code: 'ERR_TEST_FAILURE'
|
||||
@@ -265,6 +265,28 @@
|
||||
*
|
||||
*
|
||||
|
||||
assertion errors display actual and expected properly (*ms)
|
||||
AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal:
|
||||
|
||||
{
|
||||
bar: 1,
|
||||
foo: 1
|
||||
}
|
||||
|
||||
should loosely deep-equal
|
||||
|
||||
<ref *1> {
|
||||
bar: 2,
|
||||
c: [Circular *1]
|
||||
}
|
||||
* {
|
||||
generatedMessage: true,
|
||||
code: 'ERR_ASSERTION',
|
||||
actual: [Object],
|
||||
expected: [Object],
|
||||
operator: 'deepEqual'
|
||||
}
|
||||
|
||||
invalid subtest fail (*ms)
|
||||
'test could not be started because its parent finished'
|
||||
|
||||
@@ -275,10 +297,10 @@
|
||||
Warning: Test "immediate reject - passes but warns" generated asynchronous activity after the test ended. This activity created the error "Error: rejected from immediate reject fail" and would have caused the test to fail, but instead triggered an unhandledRejection event.
|
||||
Warning: Test "callback called twice in different ticks" generated asynchronous activity after the test ended. This activity created the error "Error [ERR_TEST_FAILURE]: callback invoked multiple times" and would have caused the test to fail, but instead triggered an uncaughtException event.
|
||||
Warning: Test "callback async throw after done" generated asynchronous activity after the test ended. This activity created the error "Error: thrown from callback async throw after done" and would have caused the test to fail, but instead triggered an uncaughtException event.
|
||||
tests 79
|
||||
tests 80
|
||||
suites 0
|
||||
pass 37
|
||||
fail 24
|
||||
fail 25
|
||||
cancelled 3
|
||||
skipped 10
|
||||
todo 5
|
||||
@@ -490,5 +512,27 @@
|
||||
*
|
||||
*
|
||||
|
||||
assertion errors display actual and expected properly (*ms)
|
||||
AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal:
|
||||
|
||||
{
|
||||
bar: 1,
|
||||
foo: 1
|
||||
}
|
||||
|
||||
should loosely deep-equal
|
||||
|
||||
<ref *1> {
|
||||
bar: 2,
|
||||
c: [Circular *1]
|
||||
}
|
||||
* {
|
||||
generatedMessage: true,
|
||||
code: 'ERR_ASSERTION',
|
||||
actual: [Object],
|
||||
expected: [Object],
|
||||
operator: 'deepEqual'
|
||||
}
|
||||
|
||||
invalid subtest fail (*ms)
|
||||
'test could not be started because its parent finished'
|
||||
Reference in new issue
Block a user