From 970c76fd10a14a919a45d6ecfd581b318cc4a5fe Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Wed, 2 Jun 2021 15:30:45 +0000 Subject: [PATCH] Fix Eltro handling of error messages so they always become normalized as Error objects --- lib/eltro.mjs | 13 ++++++++++++- test/cli.test.mjs | 2 +- test/failure.test.mjs | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 test/failure.test.mjs diff --git a/lib/eltro.mjs b/lib/eltro.mjs index d8b308f..5d997c5 100644 --- a/lib/eltro.mjs +++ b/lib/eltro.mjs @@ -189,7 +189,18 @@ Eltro.prototype.__runTest = async function(stats, test) { .then(function() { stats.passed++ }, function(err) { - test.error = err + let saveError = err + if (!saveError) { + saveError = new Error('Test promise rejected with empty message') + } else if (typeof(saveError) !== 'object' || saveError.message == null || saveError.stack == null) { + try { + saveError = new Error('Test promise rejected with ' + JSON.stringify(saveError)) + } catch (parseError) { + saveError = new Error('Test promise rejected with ' + saveError + ' (Error stringifying: ' + parseError.message + ')') + } + saveError.originalError = err + } + test.error = saveError stats.failed++ } ) diff --git a/test/cli.test.mjs b/test/cli.test.mjs index 5ab7b5f..7c1020f 100644 --- a/test/cli.test.mjs +++ b/test/cli.test.mjs @@ -241,7 +241,7 @@ t.describe('CLI', function() { cli: true }) }) - }) + }) }) t.test('#fileMatches() should support filename matching with glob pattern', async function() { diff --git a/test/failure.test.mjs b/test/failure.test.mjs new file mode 100644 index 0000000..64ca8af --- /dev/null +++ b/test/failure.test.mjs @@ -0,0 +1,28 @@ +import e from '../lib/eltro.mjs' +import assert from '../lib/assert.mjs' + +function CreateT() { + const t = new e.Eltro() + t.reporter = '' + return t +} + +e.test('Eltro should support any value in promise fail', async function() { + const t = CreateT() + t.begin() + t.describe('', function() { + t.test('a', function() { return new Promise(function(res, rej) { rej() }) }) + t.test('b', function() { return new Promise(function(res, rej) { rej(null) }) }) + t.test('c', function() { return new Promise(function(res, rej) { rej(undefined) }) }) + t.test('d', function() { return new Promise(function(res, rej) { rej(124523) }) }) + t.test('e', function() { return new Promise(function(res, rej) { rej('testety') }) }) + }) + await t.run() + assert.strictEqual(t.failedTests.length, 5) + + for (let x = 0; x < t.failedTests.length; x++) { + assert.strictEqual(typeof(t.failedTests[x].error), 'object') + assert.ok(t.failedTests[x].error.message) + assert.ok(t.failedTests[x].error.stack) + } +})