From 2be5e0a9620bb1dff4bbe3530f481a73ef48c73c Mon Sep 17 00:00:00 2001 From: Julian Lannigan Date: Thu, 9 Nov 2017 19:43:48 -0500 Subject: [PATCH 1/3] Allowed async functions to be passed into spy#callFake --- lib/jasmine-core/jasmine.js | 2 +- spec/core/SpyStrategySpec.js | 24 ++++++++++++++++++++++++ src/core/SpyStrategy.js | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index e54501e8..f30b7b00 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -5010,7 +5010,7 @@ getJasmineRequireObj().SpyStrategy = function(j$) { * @param {Function} fn The function to invoke with the passed parameters. */ this.callFake = function(fn) { - if(!j$.isFunction_(fn)) { + if(!(j$.isFunction_(fn) || j$.isAsyncFunction_(fn))) { throw new Error('Argument passed to callFake should be a function, got ' + fn); } plan = fn; diff --git a/spec/core/SpyStrategySpec.js b/spec/core/SpyStrategySpec.js index 0965c04c..2002af76 100644 --- a/spec/core/SpyStrategySpec.js +++ b/spec/core/SpyStrategySpec.js @@ -92,16 +92,40 @@ describe("SpyStrategy", function() { expect(returnValue).toEqual(67); }); + it("allows a fake async function to be called instead", async function(done) { + try { + var originalFn = jasmine.createSpy("original"), + fakeFn = jasmine.createSpy("fake").and.callFake(async function () { return 67; }), + spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}), + returnValue; + + spyStrategy.callFake(fakeFn); + returnValue = await spyStrategy.exec(); + + expect(originalFn).not.toHaveBeenCalled(); + expect(returnValue).toEqual(67); + + done(); + } catch (err) { + done.fail(err); + } + }); + it('throws an error when a non-function is passed to callFake strategy', function() { var originalFn = jasmine.createSpy('original'), spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}), invalidFakes = [5, 'foo', {}, true, false, null, void 0, new Date(), /.*/]; spyOn(jasmineUnderTest, 'isFunction_').and.returnValue(false); + spyOn(jasmineUnderTest, 'isAsyncFunction_').and.returnValue(false); expect(function () { spyStrategy.callFake(function() {}); }).toThrowError(/^Argument passed to callFake should be a function, got/); + + expect(function () { + spyStrategy.callFake(async function() {}); + }).toThrowError(/^Argument passed to callFake should be a function, got/); }); it("allows a return to plan stubbing after another strategy", function() { diff --git a/src/core/SpyStrategy.js b/src/core/SpyStrategy.js index 5af7cbee..b0c716f7 100644 --- a/src/core/SpyStrategy.js +++ b/src/core/SpyStrategy.js @@ -88,7 +88,7 @@ getJasmineRequireObj().SpyStrategy = function(j$) { * @param {Function} fn The function to invoke with the passed parameters. */ this.callFake = function(fn) { - if(!j$.isFunction_(fn)) { + if(!(j$.isFunction_(fn) || j$.isAsyncFunction_(fn))) { throw new Error('Argument passed to callFake should be a function, got ' + fn); } plan = fn; From 7ac1244f58eb1faa37ae2862e167363b891c4b19 Mon Sep 17 00:00:00 2001 From: Julian Lannigan Date: Fri, 10 Nov 2017 10:17:48 -0500 Subject: [PATCH 2/3] changed tests to work in environments that dont support async/await --- spec/core/SpyStrategySpec.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/spec/core/SpyStrategySpec.js b/spec/core/SpyStrategySpec.js index 2002af76..16f1af18 100644 --- a/spec/core/SpyStrategySpec.js +++ b/spec/core/SpyStrategySpec.js @@ -92,23 +92,22 @@ describe("SpyStrategy", function() { expect(returnValue).toEqual(67); }); - it("allows a fake async function to be called instead", async function(done) { - try { - var originalFn = jasmine.createSpy("original"), - fakeFn = jasmine.createSpy("fake").and.callFake(async function () { return 67; }), - spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}), - returnValue; - - spyStrategy.callFake(fakeFn); - returnValue = await spyStrategy.exec(); + it("allows a fake async function to be called instead", function(done) { + jasmine.getEnv().requireAsyncAwait(); + var originalFn = jasmine.createSpy("original"), + fakeFn = jasmine.createSpy("fake").and.callFake(eval("async () => { return 67; }")), + spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}), + returnValue; + spyStrategy.callFake(fakeFn); + spyStrategy.exec().then(function (returnValue) { expect(originalFn).not.toHaveBeenCalled(); + expect(fakeFn).toHaveBeenCalled(); expect(returnValue).toEqual(67); - done(); - } catch (err) { + }).catch(function (err) { done.fail(err); - } + }) }); it('throws an error when a non-function is passed to callFake strategy', function() { @@ -124,7 +123,7 @@ describe("SpyStrategy", function() { }).toThrowError(/^Argument passed to callFake should be a function, got/); expect(function () { - spyStrategy.callFake(async function() {}); + spyStrategy.callFake(function() {}); }).toThrowError(/^Argument passed to callFake should be a function, got/); }); From 2eef0747a0301d3f355860c4ba2a4ce58239c173 Mon Sep 17 00:00:00 2001 From: Julian Lannigan Date: Fri, 10 Nov 2017 10:22:58 -0500 Subject: [PATCH 3/3] Added test steps for other major node versions addresses https://www.pivotaltracker.com/story/show/152685778 --- .travis.yml | 11 ++++++++++- travis-node-script.sh | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b8663be6..db5fb003 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,16 @@ matrix: include: - env: - USE_SAUCE=false - - TEST_COMMAND="bash travis-node-script.sh" + - TEST_COMMAND="bash travis-node-script.sh v0.12.18" + - env: + - USE_SAUCE=false + - TEST_COMMAND="bash travis-node-script.sh v4" + - env: + - USE_SAUCE=false + - TEST_COMMAND="bash travis-node-script.sh v8" + - env: + - USE_SAUCE=false + - TEST_COMMAND="bash travis-node-script.sh v9" - env: - JASMINE_BROWSER="safari" - SAUCE_OS="OS X 10.11" diff --git a/travis-node-script.sh b/travis-node-script.sh index 6fa0ded0..fe3859b7 100644 --- a/travis-node-script.sh +++ b/travis-node-script.sh @@ -4,7 +4,7 @@ rm -rf ~/.nvm git clone https://github.com/creationix/nvm.git ~/.nvm (cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`) source ~/.nvm/nvm.sh -nvm install v0.12.18 +nvm install ${1:-"v0.12.18"} npm install npm test