Skip to content

Commit

Permalink
Fixed bug where expect.fail threw an error with no message.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Aug 21, 2014
1 parent f7d6d34 commit 510db5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/Unexpected.js
Expand Up @@ -121,9 +121,10 @@ Unexpected.prototype.fail = function (arg) {
});
}

var error = new Error();
var error = new Error();
error._isUnexpected = true;
error.output = output;
this.serializeOutputToMessage(error);
throw error;
};

Expand Down Expand Up @@ -270,7 +271,7 @@ function handleNestedExpects(e, assertion) {
case 'default':
return errorWithMessage(e, assertion.standardErrorMessage());
case 'bubble':
return e;
return errorWithMessage(e, e.output);
default:
throw new Error("Unknown error mode: '" + assertion.errorMode + "'");
}
Expand Down Expand Up @@ -299,6 +300,15 @@ function makeExpectFunction(unexpected) {
return expect;
}

Unexpected.prototype.serializeOutputToMessage = function (err) {
var outputFormat = this.outputFormat();
if (outputFormat === 'html') {
outputFormat = 'text';
err.htmlMessage = err.output.toString('html');
}
err.message = err.output.toString(outputFormat);
};

Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
var that = this;
if (arguments.length < 2) {
Expand All @@ -323,12 +333,7 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
truncateStack(e, wrappedExpect);
if (nestingLevel === 0) {
var wrappedError = handleNestedExpects(e, assertion);
var outputFormat = that.outputFormat();
if (outputFormat === 'html') {
outputFormat = 'text';
wrappedError.htmlMessage = wrappedError.output.toString('html');
}
wrappedError.message = wrappedError.output.toString(outputFormat);
that.serializeOutputToMessage(wrappedError);
throw wrappedError;
}
}
Expand Down Expand Up @@ -369,10 +374,13 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
try {
handler.apply(assertion, args);
} catch (e) {
if (e._isUnexpected) {
truncateStack(e, this.expect);
var err = e;
if (err._isUnexpected) {
err = errorWithMessage(err, err.output);
that.serializeOutputToMessage(err);
truncateStack(err, this.expect);
}
throw e;
throw err;
}
} else {
var similarAssertions = this.findAssertionSimilarTo(testDescriptionString);
Expand Down
11 changes: 11 additions & 0 deletions test/unexpected.spec.js
Expand Up @@ -1048,6 +1048,17 @@ describe('unexpected', function () {
}, 'to throw exception', "explicit failure");
});

it('sets the error message', function () {
var wasCaught = false;
try {
expect.fail('fail with error message');
} catch (e) {
wasCaught = true;
expect(e.message, 'to equal', 'fail with error message');
}
expect(wasCaught, 'to be true');
});

it('throws an error with a given message', function () {
expect(function () {
expect.fail('fail with error message');
Expand Down

0 comments on commit 510db5d

Please sign in to comment.