diff --git a/lib/run-status.js b/lib/run-status.js index 8164bc08e..70791c67f 100644 --- a/lib/run-status.js +++ b/lib/run-status.js @@ -18,10 +18,9 @@ function sum(arr, key) { } function normalizeError(err) { - if (!isObj(err)) { + if (!isObj(err) || typeof err.message !== 'string') { err = { - message: err, - stack: err + message: String(err) }; } diff --git a/lib/serialize-error.js b/lib/serialize-error.js index 6a8c4947b..cbb0a2ac4 100644 --- a/lib/serialize-error.js +++ b/lib/serialize-error.js @@ -20,6 +20,11 @@ function filter(propertyName, isRoot, source, target) { return true; } + if ((propertyName === 'name' || propertyName === 'stack') && + typeof source[propertyName] !== 'string') { + return false; + } + if (propertyName === 'stack') { target.stack = beautifyStack(source.stack); return false; diff --git a/test/run-status.js b/test/run-status.js index 7783abe5f..9c6a5e5ac 100644 --- a/test/run-status.js +++ b/test/run-status.js @@ -87,3 +87,32 @@ test('calculate remaining test count', t => { t.end(); }); +test('handle non-object rejections', t => { + const runStatus = new RunStatus(); + + runStatus.on('error', err => { + t.deepEqual(err, { + file: 'foo.js', + message: '42', + type: 'rejection' + }); + t.end(); + }); + + runStatus.handleRejections({file: 'foo.js', rejections: [42]}); +}); + +test('handle non-object exceptions', t => { + const runStatus = new RunStatus(); + + runStatus.on('error', err => { + t.deepEqual(err, { + file: 'bar.js', + message: '/ab/g', + type: 'exception' + }); + t.end(); + }); + + runStatus.handleExceptions({file: 'bar.js', exception: /ab/g}); +}); diff --git a/test/serialize-error.js b/test/serialize-error.js index 27320d85f..305ebd36d 100644 --- a/test/serialize-error.js +++ b/test/serialize-error.js @@ -210,3 +210,14 @@ test('does not call toJSON() when serializing actual and expected', t => { t.notSame(serializedErr.actual, serializedErr.expected); t.end(); }); + +test('remove non-string error properties', t => { + const err = { + name: [42], + stack: /re/g + }; + const serializedErr = serialize(err); + t.is(serializedErr.name, undefined); + t.is(serializedErr.stack, undefined); + t.end(); +});