Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 88eaea8

Browse files
committed
test(matchers): update toThrow matcher
1 parent 4179f62 commit 88eaea8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Diff for: test/matchers.js

+35
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,38 @@ beforeEach(function() {
151151

152152
});
153153
});
154+
155+
156+
// TODO(vojta): remove this once Jasmine in Karma gets updated
157+
// https://github.com/pivotal/jasmine/blob/c40b64a24c607596fa7488f2a0ddb98d063c872a/src/core/Matchers.js#L217-L246
158+
// This toThrow supports RegExps.
159+
jasmine.Matchers.prototype.toThrow = function(expected) {
160+
var result = false;
161+
var exception, exceptionMessage;
162+
if (typeof this.actual != 'function') {
163+
throw new Error('Actual is not a function');
164+
}
165+
try {
166+
this.actual();
167+
} catch (e) {
168+
exception = e;
169+
}
170+
171+
if (exception) {
172+
exceptionMessage = exception.message || exception;
173+
result = (isUndefined(expected) || this.env.equals_(exceptionMessage, expected.message || expected) || (jasmine.isA_("RegExp", expected) && expected.test(exceptionMessage)));
174+
}
175+
176+
var not = this.isNot ? "not " : "";
177+
var regexMatch = jasmine.isA_("RegExp", expected) ? " an exception matching" : "";
178+
179+
this.message = function() {
180+
if (exception) {
181+
return ["Expected function " + not + "to throw" + regexMatch, expected ? expected.message || expected : "an exception", ", but it threw", exceptionMessage].join(' ');
182+
} else {
183+
return "Expected function to throw an exception.";
184+
}
185+
};
186+
187+
return result;
188+
};

0 commit comments

Comments
 (0)