From 96786c793fcba2a3fe3a182a65c97cd6ee57a7db Mon Sep 17 00:00:00 2001 From: Elliot Nelson Date: Sun, 2 Jun 2019 09:05:45 -0400 Subject: [PATCH 1/2] Allow users to set a default spy strategy --- .../integration/DefaultSpyStrategySpec.js | 68 +++++++++++++++++++ src/core/Env.js | 20 +++++- src/core/Spy.js | 6 +- src/core/SpyFactory.js | 4 +- src/core/requireInterface.js | 16 +++++ 5 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 spec/core/integration/DefaultSpyStrategySpec.js diff --git a/spec/core/integration/DefaultSpyStrategySpec.js b/spec/core/integration/DefaultSpyStrategySpec.js new file mode 100644 index 00000000..0b02c572 --- /dev/null +++ b/spec/core/integration/DefaultSpyStrategySpec.js @@ -0,0 +1,68 @@ +describe('Default Spy Strategy (Integration)', function() { + var env; + + beforeEach(function() { + env = new jasmineUnderTest.Env(); + env.configure({random: false}); + }); + + it('allows defining a default spy strategy', function(done) { + env.describe('suite with default strategy', function() { + env.beforeEach(function() { + env.setDefaultSpyStrategy(function (and) { + and.returnValue(42); + }); + }); + + env.it('spec in suite', function() { + var spy = env.createSpy('something'); + expect(spy()).toBe(42); + }); + }); + + env.it('spec not in suite', function() { + var spy = env.createSpy('something'); + expect(spy()).toBeUndefined(); + }); + + function jasmineDone(result) { + expect(result.overallStatus).toEqual('passed'); + done(); + } + + env.addReporter({ jasmineDone: jasmineDone }); + env.execute(); + }); + + it('uses the default spy strategy defined when the spy is created', function (done) { + env.it('spec', function() { + var a = env.createSpy('a'); + env.setDefaultSpyStrategy(function (and) { and.returnValue(42); }); + var b = env.createSpy('b'); + env.setDefaultSpyStrategy(function (and) { and.stub(); }); + var c = env.createSpy('c'); + env.setDefaultSpyStrategy(); + var d = env.createSpy('d'); + + expect(a()).toBeUndefined(); + expect(b()).toBe(42); + expect(c()).toBeUndefined(); + expect(d()).toBeUndefined(); + + // Check our assumptions about which spies are "configured" (this matters because + // spies that use withArgs() behave differently if they are not configured). + expect(a.and.isConfigured()).toBe(false); + expect(b.and.isConfigured()).toBe(true); + expect(c.and.isConfigured()).toBe(true); + expect(d.and.isConfigured()).toBe(false); + }); + + function jasmineDone(result) { + expect(result.overallStatus).toEqual('passed'); + done(); + } + + env.addReporter({ jasmineDone: jasmineDone }); + env.execute(); + }); +}); diff --git a/src/core/Env.js b/src/core/Env.js index 18924dcd..63d86365 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -208,6 +208,13 @@ getJasmineRequireObj().Env = function(j$) { } }); + this.setDefaultSpyStrategy = function(defaultStrategyFn) { + if (!currentRunnable()) { + throw new Error('Default spy strategy must be set in a before function or a spec'); + } + runnableResources[currentRunnable().id].defaultStrategyFn = defaultStrategyFn; + }; + this.addSpyStrategy = function(name, fn) { if (!currentRunnable()) { throw new Error( @@ -286,7 +293,8 @@ getJasmineRequireObj().Env = function(j$) { spies: [], customEqualityTesters: [], customMatchers: {}, - customSpyStrategies: {} + customSpyStrategies: {}, + defaultStrategyFn: undefined }; if (runnableResources[parentRunnableId]) { @@ -296,6 +304,7 @@ getJasmineRequireObj().Env = function(j$) { resources.customMatchers = j$.util.clone( runnableResources[parentRunnableId].customMatchers ); + resources.defaultStrategyFn = runnableResources[parentRunnableId].defaultStrategyFn; } runnableResources[id] = resources; @@ -720,6 +729,15 @@ getJasmineRequireObj().Env = function(j$) { return {}; }, + function getDefaultStrategyFn() { + var runnable = currentRunnable(); + + if (runnable) { + return runnableResources[runnable.id].defaultStrategyFn; + } + + return undefined; + }, function getPromise() { return customPromise || global.Promise; } diff --git a/src/core/Spy.js b/src/core/Spy.js index 753499bb..42de6035 100644 --- a/src/core/Spy.js +++ b/src/core/Spy.js @@ -12,7 +12,7 @@ getJasmineRequireObj().Spy = function(j$) { * @constructor * @name Spy */ - function Spy(name, originalFn, customStrategies, getPromise) { + function Spy(name, originalFn, customStrategies, defaultStrategyFn, getPromise) { var numArgs = typeof originalFn === 'function' ? originalFn.length : 0, wrapper = makeFunc(numArgs, function() { return spy.apply(this, Array.prototype.slice.call(arguments)); @@ -127,6 +127,10 @@ getJasmineRequireObj().Spy = function(j$) { }; wrapper.calls = callTracker; + if (defaultStrategyFn) { + defaultStrategyFn(wrapper.and); + } + return wrapper; } diff --git a/src/core/SpyFactory.js b/src/core/SpyFactory.js index a622a8a3..984573c0 100644 --- a/src/core/SpyFactory.js +++ b/src/core/SpyFactory.js @@ -1,9 +1,9 @@ getJasmineRequireObj().SpyFactory = function(j$) { - function SpyFactory(getCustomStrategies, getPromise) { + function SpyFactory(getCustomStrategies, getDefaultStrategyFn, getPromise) { var self = this; this.createSpy = function(name, originalFn) { - return j$.Spy(name, originalFn, getCustomStrategies(), getPromise); + return j$.Spy(name, originalFn, getCustomStrategies(), getDefaultStrategyFn(), getPromise); }; this.createSpyObj = function(baseName, methodNames) { diff --git a/src/core/requireInterface.js b/src/core/requireInterface.js index cff7403b..2e509348 100644 --- a/src/core/requireInterface.js +++ b/src/core/requireInterface.js @@ -330,5 +330,21 @@ getJasmineRequireObj().interface = function(jasmine, env) { return env.addSpyStrategy(name, factory); }; + /** + * Set the default spy strategy for the current scope of specs. + * + * _Note:_ This is only callable from within a {@link beforeEach}, {@link it}, or {@link beforeAll}. + * @name jasmine.setDefaultSpyStrategy + * @function + * @param {Function} defaultStrategyFn - a function that assigns a strategy + * @example + * beforeEach(function() { + * jasmine.setDefaultSpyStrategy(and => and.returnValue(true)); + * }); + */ + jasmine.setDefaultSpyStrategy = function(defaultStrategyFn) { + return env.setDefaultSpyStrategy(defaultStrategyFn); + }; + return jasmineInterface; }; From e07da963545da8e2711a7057ed42a80e08782ca8 Mon Sep 17 00:00:00 2001 From: Elliot Nelson Date: Fri, 7 Jun 2019 23:33:06 -0400 Subject: [PATCH 2/2] Apply prettier --- src/core/Env.js | 11 ++++++++--- src/core/Spy.js | 8 +++++++- src/core/SpyFactory.js | 8 +++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/core/Env.js b/src/core/Env.js index 63d86365..52bdd4d8 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -210,9 +210,13 @@ getJasmineRequireObj().Env = function(j$) { this.setDefaultSpyStrategy = function(defaultStrategyFn) { if (!currentRunnable()) { - throw new Error('Default spy strategy must be set in a before function or a spec'); + throw new Error( + 'Default spy strategy must be set in a before function or a spec' + ); } - runnableResources[currentRunnable().id].defaultStrategyFn = defaultStrategyFn; + runnableResources[ + currentRunnable().id + ].defaultStrategyFn = defaultStrategyFn; }; this.addSpyStrategy = function(name, fn) { @@ -304,7 +308,8 @@ getJasmineRequireObj().Env = function(j$) { resources.customMatchers = j$.util.clone( runnableResources[parentRunnableId].customMatchers ); - resources.defaultStrategyFn = runnableResources[parentRunnableId].defaultStrategyFn; + resources.defaultStrategyFn = + runnableResources[parentRunnableId].defaultStrategyFn; } runnableResources[id] = resources; diff --git a/src/core/Spy.js b/src/core/Spy.js index 42de6035..86d44b07 100644 --- a/src/core/Spy.js +++ b/src/core/Spy.js @@ -12,7 +12,13 @@ getJasmineRequireObj().Spy = function(j$) { * @constructor * @name Spy */ - function Spy(name, originalFn, customStrategies, defaultStrategyFn, getPromise) { + function Spy( + name, + originalFn, + customStrategies, + defaultStrategyFn, + getPromise + ) { var numArgs = typeof originalFn === 'function' ? originalFn.length : 0, wrapper = makeFunc(numArgs, function() { return spy.apply(this, Array.prototype.slice.call(arguments)); diff --git a/src/core/SpyFactory.js b/src/core/SpyFactory.js index 984573c0..21a5d465 100644 --- a/src/core/SpyFactory.js +++ b/src/core/SpyFactory.js @@ -3,7 +3,13 @@ getJasmineRequireObj().SpyFactory = function(j$) { var self = this; this.createSpy = function(name, originalFn) { - return j$.Spy(name, originalFn, getCustomStrategies(), getDefaultStrategyFn(), getPromise); + return j$.Spy( + name, + originalFn, + getCustomStrategies(), + getDefaultStrategyFn(), + getPromise + ); }; this.createSpyObj = function(baseName, methodNames) {