Skip to content

Commit 276e09d

Browse files
authored
CLI: Apply trace cleaning and internal greying to assertion stack
Follows-up #1789, which applied this to traces under uncaught errors. We now apply this clean up to traces under assertion failures as well. Closes #1795.
1 parent a952967 commit 276e09d

12 files changed

+17
-28
lines changed

src/core/reporters/TapReporter.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ export default class TapReporter {
269269
if (error.stack) {
270270
// Since stacks aren't user generated, take a bit of liberty by
271271
// adding a trailing new line to allow a straight-forward YAML Blocks.
272-
out += `\n stack: ${prettyYamlValue(error.stack + '\n')}`;
272+
const fmtStack = annotateStacktrace(error.stack, kleur.grey);
273+
if (fmtStack.length) {
274+
out += `\n stack: ${prettyYamlValue(fmtStack + '\n')}`;
275+
}
273276
}
274277

275278
out += '\n ...';
@@ -281,7 +284,10 @@ export default class TapReporter {
281284
out += `\n message: ${prettyYamlValue(errorString(error))}`;
282285
out += `\n severity: ${prettyYamlValue('failed')}`;
283286
if (error && error.stack) {
284-
out += `\n stack: ${prettyYamlValue(annotateStacktrace(error, kleur.grey) + '\n')}`;
287+
const fmtStack = annotateStacktrace(error.stack, kleur.grey, error.toString());
288+
if (fmtStack.length) {
289+
out += `\n stack: ${prettyYamlValue(fmtStack + '\n')}`;
290+
}
285291
}
286292
out += '\n ...';
287293
this.log(out);

src/core/stacktrace.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,20 @@ function qunitFileName () {
6060
const fileName = qunitFileName();
6161

6262
/**
63+
* Responsibilities:
6364
* - For internal errors from QUnit itself, remove the first qunit.js frames.
6465
* - For errors in Node.js, format any remaining qunit.js and node:internal
6566
* frames as internal (i.e. grey out).
67+
*
68+
* @param {string} Error#stack
69+
* @param {Function} formatInternal Format a string in an "internal" color
70+
* @param {string|null} [eToString] Error#toString() to help remove
71+
* noise from Node.js/V8 stack traces.
6672
*/
67-
export function annotateStacktrace (e, formatInternal) {
68-
if (!e || !e.stack) {
69-
return String(e);
70-
}
71-
const frames = e.stack.split('\n');
73+
export function annotateStacktrace (stack, formatInternal, eToString = null) {
74+
const frames = stack.split('\n');
7275
const annotated = [];
73-
if (e.toString().indexOf(frames[0]) !== -1) {
76+
if (eToString && eToString.indexOf(frames[0]) !== -1) {
7477
// In Firefox and Safari e.stack starts with frame 0, but in V8 (Chrome/Node.js),
7578
// e.stack starts first stringified message. Preserve this separately,
7679
// so that, below, we can distinguish between internal frames on top

test/cli/cli-main.js

-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ not ok 2 slow
3939
---
4040
message: Test took longer than 7ms; test timed out.
4141
severity: failed
42-
stack: |
43-
at internal
4442
...
4543
ok 3 config
4644
1..3

test/cli/fixtures/config-noglobals-add.tap.txt

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ not ok 1 adds global var
66
---
77
message: Introduced global variable(s): dummyGlobal
88
severity: failed
9-
stack: |
10-
at qunit.js
119
...
1210
1..1
1311
# pass 0

test/cli/fixtures/config-noglobals-remove.tap.txt

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ not ok 1 deletes global var
66
---
77
message: Deleted global variable(s): dummyGlobal
88
severity: failed
9-
stack: |
10-
at qunit.js
119
...
1210
1..1
1311
# pass 0

test/cli/fixtures/config-notrycatch-hook-rejection.tap.txt

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ not ok 1 example > passing test
1212
---
1313
message: Test took longer than 1000ms; test timed out.
1414
severity: failed
15-
stack: |
16-
at internal
1715
...
1816
1..1
1917
# pass 0

test/cli/fixtures/config-notrycatch-test-rejection.tap.txt

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ not ok 1 example > returns a rejected promise
1212
---
1313
message: Test took longer than 1000ms; test timed out.
1414
severity: failed
15-
stack: |
16-
at internal
1715
...
1816
1..1
1917
# pass 0

test/cli/fixtures/config-testTimeout.tap.txt

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ not ok 1 slow
66
---
77
message: Test took longer than 10ms; test timed out.
88
severity: failed
9-
stack: |
10-
at internal
119
...
1210
1..1
1311
# pass 0

test/cli/fixtures/done-after-timeout.tap.txt

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ not ok 1 times out before scheduled done is called
66
---
77
message: Test took longer than 10ms; test timed out.
88
severity: failed
9-
stack: |
10-
at internal
119
...
1210
1..1
1311
# pass 0

test/cli/fixtures/hanging-test.tap.txt

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ not ok 1 hanging
66
---
77
message: Test took longer than 3000ms; test timed out.
88
severity: failed
9-
stack: |
10-
at internal
119
...
1210
1..1
1311
# pass 0

test/cli/fixtures/pending-async-after-timeout.tap.txt

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ not ok 1 example
66
---
77
message: Test took longer than 10ms; test timed out.
88
severity: failed
9-
stack: |
10-
at internal
119
...
1210
1..1
1311
# pass 0

test/cli/fixtures/timeout.tap.txt

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ not ok 1 timeout > first
66
---
77
message: Test took longer than 10ms; test timed out.
88
severity: failed
9-
stack: |
10-
at internal
119
...
1210
ok 2 timeout > second
1311
1..2

0 commit comments

Comments
 (0)