diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index fba0f67a..a86f54f6 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -416,10 +416,16 @@ getJasmineRequireObj().Env = function(j$) { }; this.addCustomEqualityTester = function(tester) { + if(!currentRunnable()) { + throw new Error('Custom Equalities must be added in a before function or a spec'); + } runnableResources[currentRunnable().id].customEqualityTesters.push(tester); }; this.addMatchers = function(matchersToAdd) { + if(!currentRunnable()) { + throw new Error('Matchers must be added in a before function or a spec'); + } var customMatchers = runnableResources[currentRunnable().id].customMatchers; for (var matcherName in matchersToAdd) { customMatchers[matcherName] = matchersToAdd[matcherName]; diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 182edd78..a824db22 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -825,4 +825,51 @@ describe("Env integration", function() { env.execute(); }); + + it('throws an exception if you try to add a matcher outside of a runnable', function (done) { + var env = new j$.Env(), + obj = {fn: function () {}}, + exception; + + env.describe("a suite", function () { + try { + env.addMatchers({myMatcher: function(actual,expected){return false;}}); + } catch(e) { + exception = e; + } + }); + + var assertions = function() { + expect(exception.message).toBe('Matchers must be added in a before function or a spec'); + done(); + }; + + env.addReporter({jasmineDone: assertions}); + + env.execute(); + }); + + it('throws an exception if you try to add a custom equality outside of a runnable', function (done) { + var env = new j$.Env(), + obj = {fn: function () {}}, + exception; + + env.describe("a suite", function () { + try { + env.addCustomEqualityTester(function(first, second) {return true;}); + } catch(e) { + exception = e; + } + }); + + var assertions = function() { + expect(exception.message).toBe('Custom Equalities must be added in a before function or a spec'); + done(); + }; + + env.addReporter({jasmineDone: assertions}); + + env.execute(); + }); + }); diff --git a/src/core/Env.js b/src/core/Env.js index 111c2519..f174cf22 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -42,10 +42,16 @@ getJasmineRequireObj().Env = function(j$) { }; this.addCustomEqualityTester = function(tester) { + if(!currentRunnable()) { + throw new Error('Custom Equalities must be added in a before function or a spec'); + } runnableResources[currentRunnable().id].customEqualityTesters.push(tester); }; this.addMatchers = function(matchersToAdd) { + if(!currentRunnable()) { + throw new Error('Matchers must be added in a before function or a spec'); + } var customMatchers = runnableResources[currentRunnable().id].customMatchers; for (var matcherName in matchersToAdd) { customMatchers[matcherName] = matchersToAdd[matcherName];