From bf1501feddd99ba3c5102575378cfce11917b2b0 Mon Sep 17 00:00:00 2001 From: Wout Mertens Date: Fri, 13 Apr 2018 08:48:32 +0200 Subject: [PATCH] jest-jasmine2: pretty-print non-Error errors --- packages/jest-jasmine2/package.json | 3 ++- packages/jest-jasmine2/src/jasmine/Env.js | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/jest-jasmine2/package.json b/packages/jest-jasmine2/package.json index 4e260e5f98dc..f9c57cd5f53d 100644 --- a/packages/jest-jasmine2/package.json +++ b/packages/jest-jasmine2/package.json @@ -18,7 +18,8 @@ "jest-message-util": "^22.4.0", "jest-snapshot": "^22.4.0", "jest-util": "^22.4.1", - "source-map-support": "^0.5.0" + "source-map-support": "^0.5.0", + "pretty-format": "^22.4.0" }, "devDependencies": { "jest-runtime": "^22.4.2" diff --git a/packages/jest-jasmine2/src/jasmine/Env.js b/packages/jest-jasmine2/src/jasmine/Env.js index a39d2a2aa41d..f0b8fad64230 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.js +++ b/packages/jest-jasmine2/src/jasmine/Env.js @@ -32,6 +32,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import queueRunner from '../queue_runner'; import treeProcessor from '../tree_processor'; +import prettyFormat from 'pretty-format'; // Try getting the real promise object from the context, if available. Someone // could have overridden it in a test. Async functions return it implicitly. @@ -548,9 +549,18 @@ export default function(j$) { this.fail = function(error) { let message = 'Failed'; + // duck-type Error, see #2549 + const isError = + typeof error === 'object' && + typeof error.message === 'string' && + typeof error.name === 'string'; if (error) { message += ': '; - message += error.message || error; + if (isError) { + message += error.message; + } else { + message += prettyFormat(error); + } } currentRunnable().addExpectationResult(false, { @@ -559,7 +569,7 @@ export default function(j$) { expected: '', actual: '', message, - error: error && error.message ? error : null, + error: isError ? error : null, }); }; }