diff --git a/lib/test.js b/lib/test.js index 303e0d2e7..7d2d5139b 100644 --- a/lib/test.js +++ b/lib/test.js @@ -9,6 +9,16 @@ const nowAndTimers = require('./now-and-timers'); const parseTestArgs = require('./parse-test-args'); const concordanceOptions = require('./concordance-options').default; +function isExternalAssertError(error) { + // Match errors thrown by . + if (error.matcherResult) { + return error.matcherResult.actual && error.matcherResult.expected; + } + + // Match errors thrown by and . + return error.actual && error.expected; +} + function formatErrorValue(label, error) { const formatted = concordance.format(error, concordanceOptions); return {label, formatted}; @@ -565,11 +575,19 @@ class Test { const result = this.callFn(); if (!result.ok) { if (!this.detectImproperThrows(result.error)) { - this.saveFirstError(new assert.AssertionError({ - message: 'Error thrown in test', - savedError: result.error instanceof Error && result.error, - values: [formatErrorValue('Error thrown in test:', result.error)] - })); + if (isExternalAssertError(result.error)) { + this.saveFirstError(new assert.AssertionError({ + message: 'Assertion failed', + savedError: result.error instanceof Error && result.error, + values: [{label: 'Assertion failed: ', formatted: result.error.message}] + })); + } else { + this.saveFirstError(new assert.AssertionError({ + message: 'Error thrown in test', + savedError: result.error instanceof Error && result.error, + values: [formatErrorValue('Error thrown in test:', result.error)] + })); + } } return this.finishPromised(); @@ -638,11 +656,19 @@ class Test { promise .catch(error => { if (!this.detectImproperThrows(error)) { - this.saveFirstError(new assert.AssertionError({ - message: 'Rejected promise returned by test', - savedError: error instanceof Error && error, - values: [formatErrorValue('Rejected promise returned by test. Reason:', error)] - })); + if (isExternalAssertError(error)) { + this.saveFirstError(new assert.AssertionError({ + message: 'Assertion failed', + savedError: error instanceof Error && error, + values: [{label: 'Assertion failed: ', formatted: error.message}] + })); + } else { + this.saveFirstError(new assert.AssertionError({ + message: 'Rejected promise returned by test', + savedError: error instanceof Error && error, + values: [formatErrorValue('Rejected promise returned by test. Reason:', error)] + })); + } } }) .then(() => resolve(this.finishPromised())); // eslint-disable-line promise/prefer-await-to-then