From ab133d7f546b4be83b7277e731369c00d3905f67 Mon Sep 17 00:00:00 2001 From: Anton Harniakou Date: Thu, 22 Aug 2019 09:01:02 +0300 Subject: [PATCH 1/3] Format failed external asserts nicer External asserts like: - chai - expect Closes #1277 --- lib/test.js | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/test.js b/lib/test.js index 10b277ec8..dc487a77f 100644 --- a/lib/test.js +++ b/lib/test.js @@ -8,6 +8,14 @@ const assert = require('./assert'); const nowAndTimers = require('./now-and-timers'); const concordanceOptions = require('./concordance-options').default; +function isExternalAssertError(error) { + if (error.matcherResult) { + return error.matcherResult.actual && error.matcherResult.expected; + } + + return error.actual && error.expected; +} + function formatErrorValue(label, error) { const formatted = concordance.format(error, concordanceOptions); return {label, formatted}; @@ -362,11 +370,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(); @@ -435,11 +451,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 From 74046efd9503877c3c6b3a49c946f72c468d48f2 Mon Sep 17 00:00:00 2001 From: Anton Harniakou Date: Tue, 10 Sep 2019 11:18:31 +0300 Subject: [PATCH 2/3] Add comment for which error is which --- lib/test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/test.js b/lib/test.js index dc487a77f..1c279db4c 100644 --- a/lib/test.js +++ b/lib/test.js @@ -9,10 +9,12 @@ const nowAndTimers = require('./now-and-timers'); const concordanceOptions = require('./concordance-options').default; function isExternalAssertError(error) { + // expect errors if (error.matcherResult) { return error.matcherResult.actual && error.matcherResult.expected; } + // chai, assert errors return error.actual && error.expected; } From d5156765bbc6e799457ee1b368fcb946d2afe133 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sun, 15 Sep 2019 18:43:15 +0200 Subject: [PATCH 3/3] Fix comment indentation, reference origin library by URL not name --- lib/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/test.js b/lib/test.js index 1c279db4c..3c9a5278d 100644 --- a/lib/test.js +++ b/lib/test.js @@ -9,12 +9,12 @@ const nowAndTimers = require('./now-and-timers'); const concordanceOptions = require('./concordance-options').default; function isExternalAssertError(error) { - // expect errors + // Match errors thrown by . if (error.matcherResult) { return error.matcherResult.actual && error.matcherResult.expected; } - // chai, assert errors + // Match errors thrown by and . return error.actual && error.expected; }