diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 93e5883f..67fb411c 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -2630,11 +2630,15 @@ getJasmineRequireObj().toHaveBeenCalledWith = function(j$) { return toHaveBeenCalledWith; }; -getJasmineRequireObj().toMatch = function() { +getJasmineRequireObj().toMatch = function(j$) { function toMatch() { return { compare: function(actual, expected) { + if (!j$.isString_(expected) && !j$.isA_('RegExp', expected)) { + throw new Error('Expected is not a String or a RegExp'); + } + var regexp = new RegExp(expected); return { diff --git a/spec/core/matchers/toMatchSpec.js b/spec/core/matchers/toMatchSpec.js index ae4c2e8e..dee3e774 100644 --- a/spec/core/matchers/toMatchSpec.js +++ b/spec/core/matchers/toMatchSpec.js @@ -30,5 +30,13 @@ describe("toMatch", function() { result = matcher.compare('bar', 'foo'); expect(result.pass).toBe(false); }); + + it("throws an Error when the expected is not a String or RegExp", function() { + var matcher = j$.matchers.toMatch(); + + expect(function() { + matcher.compare('foo', { bar: 'baz' }); + }).toThrowError('Expected is not a String or a RegExp'); + }); }); diff --git a/src/core/matchers/toMatch.js b/src/core/matchers/toMatch.js index 38309fd0..df7ded5e 100644 --- a/src/core/matchers/toMatch.js +++ b/src/core/matchers/toMatch.js @@ -1,8 +1,12 @@ -getJasmineRequireObj().toMatch = function() { +getJasmineRequireObj().toMatch = function(j$) { function toMatch() { return { compare: function(actual, expected) { + if (!j$.isString_(expected) && !j$.isA_('RegExp', expected)) { + throw new Error('Expected is not a String or a RegExp'); + } + var regexp = new RegExp(expected); return {