Skip to content

Commit

Permalink
jest-jasmine2: pretty-print non-Error errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Apr 13, 2018
1 parent a0bf957 commit bf1501f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 12 additions & 2 deletions packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, {
Expand All @@ -559,7 +569,7 @@ export default function(j$) {
expected: '',
actual: '',
message,
error: error && error.message ? error : null,
error: isError ? error : null,
});
};
}
Expand Down

0 comments on commit bf1501f

Please sign in to comment.