fixes #120 - fixed cryptic error messages when a test fails on uncaught exception
Showing
2 changed files
with
53 additions
and
32 deletions
... | @@ -67,8 +67,8 @@ var Tester = function Tester(casper, options) { | ... | @@ -67,8 +67,8 @@ var Tester = function Tester(casper, options) { |
67 | 67 | ||
68 | // events | 68 | // events |
69 | casper.on('step.error', function onStepError(e) { | 69 | casper.on('step.error', function onStepError(e) { |
70 | casper.test.fail(e); | 70 | this.test.uncaughtError(e, this.test.currentTestFile); |
71 | casper.test.done(); | 71 | this.test.done(); |
72 | }); | 72 | }); |
73 | 73 | ||
74 | this.on('success', function onSuccess(success) { | 74 | this.on('success', function onSuccess(success) { |
... | @@ -87,19 +87,9 @@ var Tester = function Tester(casper, options) { | ... | @@ -87,19 +87,9 @@ var Tester = function Tester(casper, options) { |
87 | * @param Boolean subject | 87 | * @param Boolean subject |
88 | * @param String message Test description | 88 | * @param String message Test description |
89 | */ | 89 | */ |
90 | this.assert = this.assertTrue = function assert(subject, message) { | 90 | this.assert = this.assertTrue = function assert(subject, message, context) { |
91 | var status = this.options.passText, eventName; | 91 | this.processAssertionResult(utils.mergeObjects({ |
92 | if (subject === true) { | 92 | success: subject === true, |
93 | eventName = 'success'; | ||
94 | style = 'INFO'; | ||
95 | this.testResults.passed++; | ||
96 | } else { | ||
97 | eventName = 'fail'; | ||
98 | status = this.options.failText; | ||
99 | style = 'RED_BAR'; | ||
100 | this.testResults.failed++; | ||
101 | } | ||
102 | this.emit(eventName, { | ||
103 | type: "assert", | 93 | type: "assert", |
104 | details: "test failed", | 94 | details: "test failed", |
105 | message: message, | 95 | message: message, |
... | @@ -107,8 +97,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -107,8 +97,7 @@ var Tester = function Tester(casper, options) { |
107 | values: { | 97 | values: { |
108 | subject: subject | 98 | subject: subject |
109 | } | 99 | } |
110 | }); | 100 | }, context || {})); |
111 | casper.echo([this.colorize(status, style), this.formatMessage(message)].join(' ')); | ||
112 | }; | 101 | }; |
113 | 102 | ||
114 | /** | 103 | /** |
... | @@ -414,7 +403,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -414,7 +403,7 @@ var Tester = function Tester(casper, options) { |
414 | } catch (e) { | 403 | } catch (e) { |
415 | // do not abort the whole suite, just fail fast displaying the | 404 | // do not abort the whole suite, just fail fast displaying the |
416 | // caught error and process next suite | 405 | // caught error and process next suite |
417 | this.fail(e); | 406 | this.uncaughtError(e, file); |
418 | this.done(); | 407 | this.done(); |
419 | } | 408 | } |
420 | }; | 409 | }; |
... | @@ -424,8 +413,8 @@ var Tester = function Tester(casper, options) { | ... | @@ -424,8 +413,8 @@ var Tester = function Tester(casper, options) { |
424 | * | 413 | * |
425 | * @param String message | 414 | * @param String message |
426 | */ | 415 | */ |
427 | this.fail = function fail(message) { | 416 | this.fail = function fail(message, context) { |
428 | this.assert(false, message); | 417 | this.assert(false, message, context); |
429 | }; | 418 | }; |
430 | 419 | ||
431 | /** | 420 | /** |
... | @@ -486,6 +475,28 @@ var Tester = function Tester(casper, options) { | ... | @@ -486,6 +475,28 @@ var Tester = function Tester(casper, options) { |
486 | }; | 475 | }; |
487 | 476 | ||
488 | /** | 477 | /** |
478 | * Processes an assertion result. | ||
479 | * | ||
480 | * @param Object result An assertion result object | ||
481 | */ | ||
482 | this.processAssertionResult = function processAssertionResult(result) { | ||
483 | var eventName, style, status; | ||
484 | if (result.success === true) { | ||
485 | eventName = 'success'; | ||
486 | style = 'INFO'; | ||
487 | status = this.options.passText; | ||
488 | this.testResults.passed++; | ||
489 | } else { | ||
490 | eventName = 'fail'; | ||
491 | style = 'RED_BAR'; | ||
492 | status = this.options.failText; | ||
493 | this.testResults.failed++; | ||
494 | } | ||
495 | this.emit(eventName, result); | ||
496 | casper.echo([this.colorize(status, style), this.formatMessage(result.message)].join(' ')); | ||
497 | }; | ||
498 | |||
499 | /** | ||
489 | * Renders a detailed report for each failed test. | 500 | * Renders a detailed report for each failed test. |
490 | * | 501 | * |
491 | * @param Array failures | 502 | * @param Array failures |
... | @@ -496,16 +507,12 @@ var Tester = function Tester(casper, options) { | ... | @@ -496,16 +507,12 @@ var Tester = function Tester(casper, options) { |
496 | } | 507 | } |
497 | casper.echo(f("\nDetails for the %d failed test%s:\n", failures.length, failures.length > 1 ? "s" : ""), "PARAMETER"); | 508 | casper.echo(f("\nDetails for the %d failed test%s:\n", failures.length, failures.length > 1 ? "s" : ""), "PARAMETER"); |
498 | failures.forEach(function _forEach(failure) { | 509 | failures.forEach(function _forEach(failure) { |
499 | var message, line; | 510 | var type, message, line; |
500 | if (utils.isType(failure.message, "object") && failure.message.stack) { | 511 | type = failure.type || "unknown"; |
501 | line = failure.message.line ? failure.message.line : 0; | 512 | line = ~~failure.line; |
502 | message = failure.message.stack; | ||
503 | } else { | ||
504 | line = 0; | ||
505 | message = failure.message; | 513 | message = failure.message; |
506 | } | ||
507 | casper.echo(f('In %s:%d', failure.file, line)); | 514 | casper.echo(f('In %s:%d', failure.file, line)); |
508 | casper.echo(f(' %s', message), "COMMENT"); | 515 | casper.echo(f(' %s: %s', type, message || "(no message was entered)"), "COMMENT"); |
509 | }); | 516 | }); |
510 | }; | 517 | }; |
511 | 518 | ||
... | @@ -598,7 +605,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -598,7 +605,7 @@ var Tester = function Tester(casper, options) { |
598 | try { | 605 | try { |
599 | this.exec(testFile); | 606 | this.exec(testFile); |
600 | } catch (e) { | 607 | } catch (e) { |
601 | this.fail(e); | 608 | this.uncaughtError(e, testFile); |
602 | this.done(); | 609 | this.done(); |
603 | } | 610 | } |
604 | }; | 611 | }; |
... | @@ -630,6 +637,22 @@ var Tester = function Tester(casper, options) { | ... | @@ -630,6 +637,22 @@ var Tester = function Tester(casper, options) { |
630 | } | 637 | } |
631 | return v1 === v2; | 638 | return v1 === v2; |
632 | }; | 639 | }; |
640 | |||
641 | /** | ||
642 | * Processes an error caught while running tests contained in a given test | ||
643 | * file. | ||
644 | * | ||
645 | * @param Error|String error The error | ||
646 | * @param String file Test file where the error occured | ||
647 | */ | ||
648 | this.uncaughtError = function uncaughtError(error, file) { | ||
649 | this.processAssertionResult({ | ||
650 | success: false, | ||
651 | type: "uncaughtError", | ||
652 | file: this.currentTestFile, | ||
653 | message: utils.isObject(error) ? error.message : error | ||
654 | }); | ||
655 | }; | ||
633 | }; | 656 | }; |
634 | 657 | ||
635 | // Tester class is an EventEmitter | 658 | // Tester class is an EventEmitter | ... | ... |
... | @@ -6,9 +6,7 @@ if (!phantom.casperLoaded) { | ... | @@ -6,9 +6,7 @@ if (!phantom.casperLoaded) { |
6 | var fs = require('fs'); | 6 | var fs = require('fs'); |
7 | var utils = require('utils'); | 7 | var utils = require('utils'); |
8 | var f = utils.format; | 8 | var f = utils.format; |
9 | var casper = require('casper').create({ | 9 | var casper = require('casper').create(); |
10 | faultTolerant: false | ||
11 | }); | ||
12 | 10 | ||
13 | // Options from cli | 11 | // Options from cli |
14 | casper.options.verbose = casper.cli.get('direct') || false; | 12 | casper.options.verbose = casper.cli.get('direct') || false; | ... | ... |
-
Please register or sign in to post a comment