Skip to content

Commit

Permalink
ThrowException can accept string to match exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
janmarek committed Jan 18, 2015
1 parent 9f0efa2 commit 857999e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ expect(fn).to.throwException(function (e) { // get the exception object
expect(e).to.be.a(SyntaxError);
});
expect(fn).to.throwException(/matches the exception message/);
expect(fn).to.throwException('exactly matches the exception message');
expect(fn2).to.not.throwException();
```

Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,20 @@
} else {
expect(subject).to.match(fn);
}
} else if ('string' == typeof fn) {
var subject = 'string' == typeof e ? e : e.message;
if (not) {
expect(subject).not.to.equal(fn);
} else {
expect(subject).to.equal(fn);
}
} else if ('function' == typeof fn) {
fn(e);
}
thrown = true;
}

if (isRegExp(fn) && not) {
if ((isRegExp(fn) && not) || ('string' == typeof fn && not)) {
// in the presence of a matcher, ensure the `not` only applies to
// the matching.
this.flags.not = false;
Expand Down
7 changes: 7 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ describe('expect', function () {
expect(itThrowsMessage).to.throwException(/no match/);
}, 'expected \'tobi\' to match /no match/');

expect(itThrowsMessage).to.throwException('tobi');
expect(itThrowsMessage).to.not.throwException('test');

err(function () {
expect(itThrowsMessage).to.throwException('no match');
}, 'expected \'tobi\' to equal \'no match\'');

var subject2;

expect(itThrowsString).to.throwException(function (str) {
Expand Down

0 comments on commit 857999e

Please sign in to comment.