Skip to content

Commit

Permalink
Merge 8c5cb03 into f7cc131
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jun 19, 2015
2 parents f7cc131 + 8c5cb03 commit 0fb9f79
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
26 changes: 12 additions & 14 deletions lib/Unexpected.js
Expand Up @@ -273,8 +273,7 @@ Unexpected.prototype.fail = function (arg) {
throw arg;
}
var output = this.output.clone();
var createDiff = null;
var label = null;
var additionalProperties = {};
if (typeof arg === 'function') {
arg.call(output, output);
} else if (arg && typeof arg === 'object') {
Expand All @@ -289,12 +288,14 @@ Unexpected.prototype.fail = function (arg) {
} else {
output.error('Explicit failure');
}
if (typeof arg.diff === 'function') {
createDiff = arg.diff;
}
if (typeof arg.label === 'string') {
label = arg.label;
}
Object.keys(arg).forEach(function (key) {
var value = arg[key];
if (key === 'diff') {
additionalProperties.createDiff = value;
} else if (key !== 'message') {
additionalProperties[key] = value;
}
});
} else {
var that = this;
var message = arg ? String(arg) : 'Explicit failure';
Expand Down Expand Up @@ -323,12 +324,9 @@ Unexpected.prototype.fail = function (arg) {

var error = new UnexpectedError(this.expect);
error.output = output;
if (createDiff) {
error.createDiff = createDiff;
}
if (typeof label === 'string') {
error.label = label;
}
Object.keys(additionalProperties).forEach(function (key) {
error[key] = additionalProperties[key];
});
throw error;
};

Expand Down
24 changes: 24 additions & 0 deletions test/unexpected.spec.js
Expand Up @@ -5855,6 +5855,30 @@ describe('unexpected', function () {
});
});

describe('fail', function () {
describe('with an object', function () {
it('should support specifying a label', function () {
expect(function () {
expect.fail({
label: 'to yadda'
});
}, 'to throw', {
label: 'to yadda'
});
});

it('should set additional properties on the thrown error', function () {
expect(function () {
expect.fail({
foobarquux: 123
});
}, 'to throw', {
foobarquux: 123
});
});
});
});

describe('async', function () {
before(function () {
expect = expect.clone()
Expand Down

0 comments on commit 0fb9f79

Please sign in to comment.