From 857999ef3e0b2c6d46bd0dcb3c0963bb1648cf5b Mon Sep 17 00:00:00 2001 From: Jan Marek Date: Sat, 1 Nov 2014 23:14:35 +0100 Subject: [PATCH] ThrowException can accept string to match exception message --- README.md | 1 + index.js | 9 ++++++++- test/expect.js | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7f5b5df..be54367 100644 --- a/README.md +++ b/README.md @@ -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(); ``` diff --git a/index.js b/index.js index 04d8371..34564f0 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/test/expect.js b/test/expect.js index 0185388..8acb631 100644 --- a/test/expect.js +++ b/test/expect.js @@ -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) {