From 305252f5a8d9806c0387699d23aed94abf246336 Mon Sep 17 00:00:00 2001 From: slackersoft Date: Tue, 16 Dec 2014 11:50:45 -0800 Subject: [PATCH] toMatch requires the `expected` to be a String or RegExp - Otherwise it was using the `toString` as the RegExp, which is almost definitely _not_ what you want. Fixes #723 --- lib/jasmine-core/jasmine.js | 6 +++++- spec/core/matchers/toMatchSpec.js | 8 ++++++++ src/core/matchers/toMatch.js | 6 +++++- 3 files changed, 18 insertions(+), 2 deletions(-) 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 {