Skip to content

Commit 5558367

Browse files
Recognize typical assertion errors and use their formatting
Co-authored-by: Mark Wubben <[email protected]>
1 parent faa9654 commit 5558367

File tree

10 files changed

+653
-14
lines changed

10 files changed

+653
-14
lines changed

lib/test.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ import concordanceOptions from './concordance-options.js';
77
import nowAndTimers from './now-and-timers.cjs';
88
import parseTestArgs from './parse-test-args.js';
99

10+
const hasOwnProperty = (object, prop) => Object.prototype.hasOwnProperty.call(object, prop);
11+
12+
function isExternalAssertError(error) {
13+
if (typeof error !== 'object' || error === null) {
14+
return false;
15+
}
16+
17+
// Match errors thrown by <https://www.npmjs.com/package/expect>.
18+
if (hasOwnProperty(error, 'matcherResult')) {
19+
return true;
20+
}
21+
22+
// Match errors thrown by <https://www.npmjs.com/package/chai> and <https://nodejs.org/api/assert.html>.
23+
return hasOwnProperty(error, 'actual') && hasOwnProperty(error, 'expected');
24+
}
25+
1026
function formatErrorValue(label, error) {
1127
const formatted = concordance.format(error, concordanceOptions);
1228
return {label, formatted};
@@ -519,11 +535,19 @@ export default class Test {
519535

520536
const result = this.callFn();
521537
if (!result.ok) {
522-
this.saveFirstError(new AssertionError({
523-
message: 'Error thrown in test',
524-
savedError: result.error instanceof Error && result.error,
525-
values: [formatErrorValue('Error thrown in test:', result.error)],
526-
}));
538+
if (isExternalAssertError(result.error)) {
539+
this.saveFirstError(new AssertionError({
540+
message: 'Assertion failed',
541+
savedError: result.error instanceof Error && result.error,
542+
values: [{label: 'Assertion failed: ', formatted: result.error.message}],
543+
}));
544+
} else {
545+
this.saveFirstError(new AssertionError({
546+
message: 'Error thrown in test',
547+
savedError: result.error instanceof Error && result.error,
548+
values: [formatErrorValue('Error thrown in test:', result.error)],
549+
}));
550+
}
527551

528552
return this.finish();
529553
}
@@ -564,11 +588,19 @@ export default class Test {
564588

565589
promise
566590
.catch(error => {
567-
this.saveFirstError(new AssertionError({
568-
message: 'Rejected promise returned by test',
569-
savedError: error instanceof Error && error,
570-
values: [formatErrorValue('Rejected promise returned by test. Reason:', error)],
571-
}));
591+
if (isExternalAssertError(error)) {
592+
this.saveFirstError(new AssertionError({
593+
message: 'Assertion failed',
594+
savedError: error instanceof Error && error,
595+
values: [{label: 'Assertion failed: ', formatted: error.message}],
596+
}));
597+
} else {
598+
this.saveFirstError(new AssertionError({
599+
message: 'Rejected promise returned by test',
600+
savedError: error instanceof Error && error,
601+
values: [formatErrorValue('Rejected promise returned by test. Reason:', error)],
602+
}));
603+
}
572604
})
573605
.then(() => resolve(this.finish()));
574606
});

0 commit comments

Comments
 (0)