diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index c064faa1..f980d6c4 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -801,6 +801,7 @@ getJasmineRequireObj().Env = function(j$) { var self = this; var global = options.global || j$.getGlobal(); + var customPromise; var totalSpecsDefined = 0; @@ -867,7 +868,16 @@ getJasmineRequireObj().Env = function(j$) { * @type Boolean * @default false */ - hideDisabled: false + hideDisabled: false, + /** + * Set to provide a custom promise library that Jasmine will use if it needs + * to create a promise. If not set, it will default to whatever global Promise + * library is available (if any). + * @name Configuration#Promise + * @type function + * @default undefined + */ + Promise: undefined }; var currentSuite = function() { @@ -933,6 +943,15 @@ getJasmineRequireObj().Env = function(j$) { if (configuration.hasOwnProperty('hideDisabled')) { config.hideDisabled = configuration.hideDisabled; } + + if (configuration.hasOwnProperty('Promise')) { + if (typeof configuration.Promise.resolve === 'function' && + typeof configuration.Promise.reject === 'function') { + customPromise = configuration.Promise; + } else { + throw new Error('Custom promise library missing `resolve`/`reject` functions'); + } + } }; /** @@ -951,11 +970,11 @@ getJasmineRequireObj().Env = function(j$) { Object.defineProperty(this, 'specFilter', { get: function() { - self.deprecated('Getting specFilter directly from Env is deprecated, please check the specFilter option from `configuration`'); + self.deprecated('Getting specFilter directly from Env is deprecated and will be removed in a future version of Jasmine, please check the specFilter option from `configuration`'); return config.specFilter; }, set: function(val) { - self.deprecated('Setting specFilter directly on Env is deprecated, please use the specFilter option in `configure`'); + self.deprecated('Setting specFilter directly on Env is deprecated and will be removed in a future version of Jasmine, please use the specFilter option in `configure`'); config.specFilter = val; } }); @@ -1091,12 +1110,12 @@ getJasmineRequireObj().Env = function(j$) { * @deprecated Use the `oneFailurePerSpec` option with {@link Env#configure} */ this.throwOnExpectationFailure = function(value) { - this.deprecated('Setting throwOnExpectationFailure directly on Env is deprecated, please use the oneFailurePerSpec option in `configure`'); + this.deprecated('Setting throwOnExpectationFailure directly on Env is deprecated and will be removed in a future version of Jasmine, please use the oneFailurePerSpec option in `configure`'); this.configure({oneFailurePerSpec: !!value}); }; this.throwingExpectationFailures = function() { - this.deprecated('Getting throwingExpectationFailures directly from Env is deprecated, please check the oneFailurePerSpec option from `configuration`'); + this.deprecated('Getting throwingExpectationFailures directly from Env is deprecated and will be removed in a future version of Jasmine, please check the oneFailurePerSpec option from `configuration`'); return config.oneFailurePerSpec; }; @@ -1108,12 +1127,12 @@ getJasmineRequireObj().Env = function(j$) { * @deprecated Use the `failFast` option with {@link Env#configure} */ this.stopOnSpecFailure = function(value) { - this.deprecated('Setting stopOnSpecFailure directly is deprecated, please use the failFast option in `configure`'); + this.deprecated('Setting stopOnSpecFailure directly is deprecated and will be removed in a future version of Jasmine, please use the failFast option in `configure`'); this.configure({failFast: !!value}); }; this.stoppingOnSpecFailure = function() { - this.deprecated('Getting stoppingOnSpecFailure directly from Env is deprecated, please check the failFast option from `configuration`'); + this.deprecated('Getting stoppingOnSpecFailure directly from Env is deprecated and will be removed in a future version of Jasmine, please check the failFast option from `configuration`'); return config.failFast; }; @@ -1125,12 +1144,12 @@ getJasmineRequireObj().Env = function(j$) { * @deprecated Use the `random` option with {@link Env#configure} */ this.randomizeTests = function(value) { - this.deprecated('Setting randomizeTests directly is deprecated, please use the random option in `configure`'); + this.deprecated('Setting randomizeTests directly is deprecated and will be removed in a future version of Jasmine, please use the random option in `configure`'); config.random = !!value; }; this.randomTests = function() { - this.deprecated('Getting randomTests directly from Env is deprecated, please check the random option from `configuration`'); + this.deprecated('Getting randomTests directly from Env is deprecated and will be removed in a future version of Jasmine, please check the random option from `configuration`'); return config.random; }; @@ -1142,7 +1161,7 @@ getJasmineRequireObj().Env = function(j$) { * @deprecated Use the `seed` option with {@link Env#configure} */ this.seed = function(value) { - this.deprecated('Setting seed directly is deprecated, please use the seed option in `configure`'); + this.deprecated('Setting seed directly is deprecated and will be removed in a future version of Jasmine, please use the seed option in `configure`'); if (value) { config.seed = value; } @@ -1150,7 +1169,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.hidingDisabled = function(value) { - this.deprecated('Getting hidingDisabled directly from Env is deprecated, please check the hideDisabled option from `configuration`'); + this.deprecated('Getting hidingDisabled directly from Env is deprecated and will be removed in a future version of Jasmine, please check the hideDisabled option from `configuration`'); return config.hideDisabled; }; @@ -1159,7 +1178,7 @@ getJasmineRequireObj().Env = function(j$) { * @function */ this.hideDisabled = function(value) { - this.deprecated('Setting hideDisabled directly is deprecated, please use the hideDisabled option in `configure`'); + this.deprecated('Setting hideDisabled directly is deprecated and will be removed in a future version of Jasmine, please use the hideDisabled option in `configure`'); config.hideDisabled = !!value; }; @@ -1411,15 +1430,20 @@ getJasmineRequireObj().Env = function(j$) { reporter.clearReporters(); }; - var spyFactory = new j$.SpyFactory(function() { - var runnable = currentRunnable(); + var spyFactory = new j$.SpyFactory( + function getCustomStrategies() { + var runnable = currentRunnable(); - if (runnable) { - return runnableResources[runnable.id].customSpyStrategies; + if (runnable) { + return runnableResources[runnable.id].customSpyStrategies; + } + + return {}; + }, + function getPromise() { + return customPromise || global.Promise; } - - return {}; - }); + ); var spyRegistry = new j$.SpyRegistry({ currentSpies: function() { @@ -6125,7 +6149,7 @@ getJasmineRequireObj().Spy = function (j$) { * @constructor * @name Spy */ - function Spy(name, originalFn, customStrategies) { + function Spy(name, originalFn, customStrategies, getPromise) { var numArgs = (typeof originalFn === 'function' ? originalFn.length : 0), wrapper = makeFunc(numArgs, function () { return spy.apply(this, Array.prototype.slice.call(arguments)); @@ -6136,7 +6160,8 @@ getJasmineRequireObj().Spy = function (j$) { getSpy: function () { return wrapper; }, - customStrategies: customStrategies + customStrategies: customStrategies, + getPromise: getPromise }), callTracker = new j$.CallTracker(), spy = function () { @@ -6210,7 +6235,6 @@ getJasmineRequireObj().Spy = function (j$) { return wrapper; } - function SpyStrategyDispatcher(strategyArgs) { var baseStrategy = new j$.SpyStrategy(strategyArgs); var argsStrategies = new StrategyDict(function() { @@ -6276,11 +6300,11 @@ getJasmineRequireObj().Spy = function (j$) { getJasmineRequireObj().SpyFactory = function(j$) { - function SpyFactory(getCustomStrategies) { + function SpyFactory(getCustomStrategies, getPromise) { var self = this; this.createSpy = function(name, originalFn) { - return j$.Spy(name, originalFn, getCustomStrategies()); + return j$.Spy(name, originalFn, getCustomStrategies(), getPromise); }; this.createSpyObj = function(baseName, methodNames) { @@ -6479,6 +6503,8 @@ getJasmineRequireObj().SpyStrategy = function(j$) { function SpyStrategy(options) { options = options || {}; + var self = this; + /** * Get the identifying information for the spy. * @name SpyStrategy#identity @@ -6496,6 +6522,48 @@ getJasmineRequireObj().SpyStrategy = function(j$) { this[k] = createCustomPlan(cs[k]); } } + + var getPromise = (typeof options.getPromise === 'function') ? options.getPromise : function() {}; + + var requirePromise = function(name) { + var Promise = getPromise(); + + if (!Promise) { + throw new Error(name + ' requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`'); + } + + return Promise; + }; + + /** + * Tell the spy to return a promise resolving to the specified value when invoked. + * @name SpyStrategy#resolveWith + * @function + * @param {*} value The value to return. + */ + this.resolveWith = function(value) { + var Promise = requirePromise('resolveWith'); + self.plan = function() { + return Promise.resolve(value); + }; + return self.getSpy(); + }; + + /** + * Tell the spy to return a promise rejecting with the specified value when invoked. + * @name SpyStrategy#rejectWith + * @function + * @param {*} value The value to return. + */ + this.rejectWith = function(value) { + var Promise = requirePromise('rejectWith'); + var error = (value instanceof Error) ? value : new Error(value); + + self.plan = function() { + return Promise.reject(error); + }; + return self.getSpy(); + }; } function createCustomPlan(factory) { diff --git a/spec/core/EnvSpec.js b/spec/core/EnvSpec.js index f731d36b..0cdad2cd 100644 --- a/spec/core/EnvSpec.js +++ b/spec/core/EnvSpec.js @@ -46,6 +46,21 @@ describe("Env", function() { })); }); + describe('promise library', function() { + it('can be configured with a custom library', function() { + var myLibrary = { resolve: jasmine.createSpy(), reject: jasmine.createSpy() }; + env.configure({ Promise: myLibrary }); + }); + + it('cannot be configured with an invalid promise library', function() { + var myLibrary = {}; + + expect(function() { + env.configure({ Promise: myLibrary }); + }).toThrowError('Custom promise library missing `resolve`/`reject` functions'); + }); + }); + it('defaults to multiple failures for specs', function() { spyOn(jasmineUnderTest, 'Spec'); env.it('bar', function() {}); diff --git a/spec/core/SpySpec.js b/spec/core/SpySpec.js index 2639aa98..04734e2f 100644 --- a/spec/core/SpySpec.js +++ b/spec/core/SpySpec.js @@ -171,7 +171,29 @@ describe('Spies', function () { expect(spy('foo')).toEqual(17); }); - describe("When withArgs is used without a base strategy", function() { + describe('any promise-based strategy', function() { + it('works with global Promise library when available', function(done) { + jasmine.getEnv().requirePromises(); + + var spy = env.createSpy('foo').and.resolveWith(42); + spy().then(function(result) { + expect(result).toEqual(42); + done(); + }).catch(done.fail); + }); + + it('works with a custom Promise library', function() { + var customPromise = { resolve: jasmine.createSpy(), reject: jasmine.createSpy() }; + customPromise.resolve.and.returnValue('resolved'); + env.configure({ Promise: customPromise }); + + var spy = env.createSpy('foo').and.resolveWith(42); + expect(spy()).toEqual('resolved'); + expect(customPromise.resolve).toHaveBeenCalledWith(42); + }); + }); + + describe("when withArgs is used without a base strategy", function() { it("uses the matching strategy", function() { var spy = env.createSpy('foo'); spy.withArgs('baz').and.returnValue(-1); diff --git a/spec/core/SpyStrategySpec.js b/spec/core/SpyStrategySpec.js index 10e4f583..709c268b 100644 --- a/spec/core/SpyStrategySpec.js +++ b/spec/core/SpyStrategySpec.js @@ -110,6 +110,70 @@ describe("SpyStrategy", function() { }) }); + describe("#resolveWith", function() { + it("allows a resolved promise to be returned", function(done) { + jasmine.getEnv().requirePromises(); + + var originalFn = jasmine.createSpy("original"), + getPromise = function() { return Promise; }, + spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn, getPromise: getPromise}); + + spyStrategy.resolveWith(37); + spyStrategy.exec().then(function (returnValue) { + expect(returnValue).toEqual(37); + done(); + }).catch(done.fail); + }); + + it("fails if promises are not available", function() { + var originalFn = jasmine.createSpy("original"), + spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}); + + expect(function() { + spyStrategy.resolveWith(37); + }).toThrowError('resolveWith requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`'); + }); + }); + + describe("#rejectWith", function() { + it("allows a rejected promise to be returned", function(done) { + jasmine.getEnv().requirePromises(); + + var originalFn = jasmine.createSpy("original"), + getPromise = function() { return Promise; }, + spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn, getPromise: getPromise}); + + spyStrategy.rejectWith(new Error('oops')); + spyStrategy.exec().then(done.fail).catch(function (error) { + expect(error).toEqual(new Error('oops')); + done(); + }).catch(done.fail); + }); + + it("allows a non-Error to be rejected, wrapping it in an exception when executed", function(done) { + jasmine.getEnv().requirePromises(); + + var originalFn = jasmine.createSpy("original"), + getPromise = function() { return Promise; }, + spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn, getPromise: getPromise}); + + spyStrategy.rejectWith('oops'); + spyStrategy.exec().then(done.fail).catch(function (error) { + expect(error).toEqual(new Error('oops')); + done(); + }).catch(done.fail); + }); + + it("fails if promises are not available", function() { + var originalFn = jasmine.createSpy("original"), + spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}); + + expect(function() { + spyStrategy.rejectWith(new Error('oops')); + }).toThrowError('rejectWith requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`'); + }); + }); + it("allows a custom strategy to be used", function() { var plan = jasmine.createSpy('custom strategy') .and.returnValue('custom strategy result'), diff --git a/src/core/Env.js b/src/core/Env.js index 82609252..e06d7546 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -10,6 +10,7 @@ getJasmineRequireObj().Env = function(j$) { var self = this; var global = options.global || j$.getGlobal(); + var customPromise; var totalSpecsDefined = 0; @@ -76,7 +77,16 @@ getJasmineRequireObj().Env = function(j$) { * @type Boolean * @default false */ - hideDisabled: false + hideDisabled: false, + /** + * Set to provide a custom promise library that Jasmine will use if it needs + * to create a promise. If not set, it will default to whatever global Promise + * library is available (if any). + * @name Configuration#Promise + * @type function + * @default undefined + */ + Promise: undefined }; var currentSuite = function() { @@ -142,6 +152,15 @@ getJasmineRequireObj().Env = function(j$) { if (configuration.hasOwnProperty('hideDisabled')) { config.hideDisabled = configuration.hideDisabled; } + + if (configuration.hasOwnProperty('Promise')) { + if (typeof configuration.Promise.resolve === 'function' && + typeof configuration.Promise.reject === 'function') { + customPromise = configuration.Promise; + } else { + throw new Error('Custom promise library missing `resolve`/`reject` functions'); + } + } }; /** @@ -620,15 +639,20 @@ getJasmineRequireObj().Env = function(j$) { reporter.clearReporters(); }; - var spyFactory = new j$.SpyFactory(function() { - var runnable = currentRunnable(); + var spyFactory = new j$.SpyFactory( + function getCustomStrategies() { + var runnable = currentRunnable(); - if (runnable) { - return runnableResources[runnable.id].customSpyStrategies; + if (runnable) { + return runnableResources[runnable.id].customSpyStrategies; + } + + return {}; + }, + function getPromise() { + return customPromise || global.Promise; } - - return {}; - }); + ); var spyRegistry = new j$.SpyRegistry({ currentSpies: function() { diff --git a/src/core/Spy.js b/src/core/Spy.js index 639d76f8..e4072f1b 100644 --- a/src/core/Spy.js +++ b/src/core/Spy.js @@ -13,7 +13,7 @@ getJasmineRequireObj().Spy = function (j$) { * @constructor * @name Spy */ - function Spy(name, originalFn, customStrategies) { + function Spy(name, originalFn, customStrategies, getPromise) { var numArgs = (typeof originalFn === 'function' ? originalFn.length : 0), wrapper = makeFunc(numArgs, function () { return spy.apply(this, Array.prototype.slice.call(arguments)); @@ -24,7 +24,8 @@ getJasmineRequireObj().Spy = function (j$) { getSpy: function () { return wrapper; }, - customStrategies: customStrategies + customStrategies: customStrategies, + getPromise: getPromise }), callTracker = new j$.CallTracker(), spy = function () { @@ -98,7 +99,6 @@ getJasmineRequireObj().Spy = function (j$) { return wrapper; } - function SpyStrategyDispatcher(strategyArgs) { var baseStrategy = new j$.SpyStrategy(strategyArgs); var argsStrategies = new StrategyDict(function() { diff --git a/src/core/SpyFactory.js b/src/core/SpyFactory.js index d4821321..4df28e55 100644 --- a/src/core/SpyFactory.js +++ b/src/core/SpyFactory.js @@ -1,10 +1,10 @@ getJasmineRequireObj().SpyFactory = function(j$) { - function SpyFactory(getCustomStrategies) { + function SpyFactory(getCustomStrategies, getPromise) { var self = this; this.createSpy = function(name, originalFn) { - return j$.Spy(name, originalFn, getCustomStrategies()); + return j$.Spy(name, originalFn, getCustomStrategies(), getPromise); }; this.createSpyObj = function(baseName, methodNames) { diff --git a/src/core/SpyStrategy.js b/src/core/SpyStrategy.js index ae5d4451..5a785d4f 100644 --- a/src/core/SpyStrategy.js +++ b/src/core/SpyStrategy.js @@ -6,6 +6,8 @@ getJasmineRequireObj().SpyStrategy = function(j$) { function SpyStrategy(options) { options = options || {}; + var self = this; + /** * Get the identifying information for the spy. * @name SpyStrategy#identity @@ -23,6 +25,48 @@ getJasmineRequireObj().SpyStrategy = function(j$) { this[k] = createCustomPlan(cs[k]); } } + + var getPromise = (typeof options.getPromise === 'function') ? options.getPromise : function() {}; + + var requirePromise = function(name) { + var Promise = getPromise(); + + if (!Promise) { + throw new Error(name + ' requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`'); + } + + return Promise; + }; + + /** + * Tell the spy to return a promise resolving to the specified value when invoked. + * @name SpyStrategy#resolveWith + * @function + * @param {*} value The value to return. + */ + this.resolveWith = function(value) { + var Promise = requirePromise('resolveWith'); + self.plan = function() { + return Promise.resolve(value); + }; + return self.getSpy(); + }; + + /** + * Tell the spy to return a promise rejecting with the specified value when invoked. + * @name SpyStrategy#rejectWith + * @function + * @param {*} value The value to return. + */ + this.rejectWith = function(value) { + var Promise = requirePromise('rejectWith'); + var error = (value instanceof Error) ? value : new Error(value); + + self.plan = function() { + return Promise.reject(error); + }; + return self.getSpy(); + }; } function createCustomPlan(factory) {