Skip to content

Commit 24651fa

Browse files
committed
ThrowException can accept string to match exception message
1 parent 564e155 commit 24651fa

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ expect(fn).to.throwException(function (e) { // get the exception object
139139
expect(e).to.be.a(SyntaxError);
140140
});
141141
expect(fn).to.throwException(/matches the exception message/);
142+
expect(fn).to.throwException('exactly matches the exception message');
142143
expect(fn2).to.not.throwException();
143144
```
144145

@@ -208,7 +209,7 @@ and shown/processed by the test runner.
208209

209210
## Differences with should.js
210211

211-
- No need for static `should` methods like `should.strictEqual`. For example,
212+
- No need for static `should` methods like `should.strictEqual`. For example,
212213
`expect(obj).to.be(undefined)` works well.
213214
- Some API simplifications / changes.
214215
- API changes related to browser compatibility.

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,20 @@
155155
} else {
156156
expect(subject).to.match(fn);
157157
}
158+
} else if ('string' == typeof fn) {
159+
var subject = 'string' == typeof e ? e : e.message;
160+
if (not) {
161+
expect(subject).not.to.equal(fn);
162+
} else {
163+
expect(subject).to.equal(fn);
164+
}
158165
} else if ('function' == typeof fn) {
159166
fn(e);
160167
}
161168
thrown = true;
162169
}
163170

164-
if (isRegExp(fn) && not) {
171+
if ((isRegExp(fn) && not) || ('string' == typeof fn && not)) {
165172
// in the presence of a matcher, ensure the `not` only applies to
166173
// the matching.
167174
this.flags.not = false;
@@ -637,7 +644,7 @@
637644
if (isDate(value) && $keys.length === 0) {
638645
return stylize(value.toUTCString(), 'date');
639646
}
640-
647+
641648
// Error objects can be shortcutted
642649
if (value instanceof Error) {
643650
return stylize("["+value.toString()+"]", 'Error');

test/expect.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ describe('expect', function () {
133133
expect(itThrowsMessage).to.throwException(/no match/);
134134
}, 'expected \'tobi\' to match /no match/');
135135

136+
expect(itThrowsMessage).to.throwException('tobi');
137+
expect(itThrowsMessage).to.not.throwException('test');
138+
139+
err(function () {
140+
expect(itThrowsMessage).to.throwException('no match');
141+
}, 'expected \'tobi\' to equal \'no match\'');
142+
136143
var subject2;
137144

138145
expect(itThrowsString).to.throwException(function (str) {
@@ -382,7 +389,7 @@ describe('expect', function () {
382389
err(function () {
383390
expect('asd').to.have.property('foo');
384391
}, "expected 'asd' to have a property 'foo'");
385-
392+
386393
err(function () {
387394
expect({ length: undefined }).to.not.have.property('length');
388395
}, "expected { length: undefined } to not have a property 'length'");
@@ -403,7 +410,7 @@ describe('expect', function () {
403410
err(function () {
404411
expect('asd').to.not.have.property('foo', 3);
405412
}, "'asd' has no property 'foo'");
406-
413+
407414
err(function () {
408415
expect({ length: undefined }).to.not.have.property('length', undefined);
409416
}, "expected { length: undefined } to not have a property 'length'");

0 commit comments

Comments
 (0)