diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 4832011e..57372e93 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -94,6 +94,7 @@ var getJasmineRequireObj = (function (jasmineGlobal) { j$.NotEmpty = jRequire.NotEmpty(j$); j$.matchers = jRequire.requireMatchers(jRequire, j$); + j$.asyncMatchers = jRequire.requireAsyncMatchers(jRequire, j$); return j$; }; @@ -969,6 +970,7 @@ getJasmineRequireObj().Env = function(j$) { }; j$.Expectation.addCoreMatchers(j$.matchers); + j$.AsyncExpectation.addCoreMatchers(j$.asyncMatchers); var nextSpecId = 0; var getNextSpecId = function() { @@ -2168,15 +2170,14 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { if (!j$.isPromiseLike(this.actual)) { throw new Error('Expected expectAsync to be called with a promise.'); } - - ['toBeResolved', 'toBeRejected', 'toBeResolvedTo', 'toBeRejectedWith'].forEach(wrapCompare.bind(this)); } - function wrapCompare(name) { - var matcher = this[name]; - this[name] = function() { + function wrapCompare(name, matcherFactory) { + return function() { var self = this; - var args = Array.prototype.slice.call(arguments); + var args = Array.prototype.slice.call(arguments), + expected = args.slice(0); + args.unshift(this.actual); // Capture the call stack here, before we go async, so that it will @@ -2184,155 +2185,48 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { // of Jasmine. var errorForStack = j$.util.errorWithStack(); - var matcherCompare = this.instantiateMatcher(matcher); + var matcherCompare = this.instantiateMatcher(matcherFactory); return matcherCompare.apply(self, args).then(function(result) { - var message; - args[0] = promiseForMessage; - message = j$.Expectation.prototype.buildMessage.call(self, result, name, args); - - self.addExpectationResult(result.pass, { - matcherName: name, - passed: result.pass, - message: message, - error: undefined, - errorForStack: errorForStack, - actual: self.actual - }); + self.processResult(result, name, expected, args, errorForStack); }); }; } - AsyncExpectation.prototype.instantiateMatcher = function(matcher) { - var comparisonFunc = this.filters.selectComparisonFunc(matcher); - return comparisonFunc || matcher; - }; + AsyncExpectation.prototype.processResult = function(result, name, expected, args, errorForStack) { + var message = this.buildMessage(result, name, args); - /** - * Expect a promise to be resolved. - * @function - * @async - * @name async-matchers#toBeResolved - * @example - * await expectAsync(aPromise).toBeResolved(); - * @example - * return expectAsync(aPromise).toBeResolved(); - */ - AsyncExpectation.prototype.toBeResolved = function(actual) { - return actual.then( - function() { return {pass: true}; }, - function() { return {pass: false}; } - ); - }; - - /** - * Expect a promise to be rejected. - * @function - * @async - * @name async-matchers#toBeRejected - * @example - * await expectAsync(aPromise).toBeRejected(); - * @example - * return expectAsync(aPromise).toBeRejected(); - */ - AsyncExpectation.prototype.toBeRejected = function(actual) { - return actual.then( - function() { return {pass: false}; }, - function() { return {pass: true}; } - ); - }; - - /** - * Expect a promise to be resolved to a value equal to the expected, using deep equality comparison. - * @function - * @async - * @name async-matchers#toBeResolvedTo - * @param {Object} expected - Value that the promise is expected to resolve to - * @example - * await expectAsync(aPromise).toBeResolvedTo({prop: 'value'}); - * @example - * return expectAsync(aPromise).toBeResolvedTo({prop: 'value'}); - */ - AsyncExpectation.prototype.toBeResolvedTo = function(actualPromise, expectedValue) { - var self = this; - - function prefix(passed) { - return 'Expected a promise ' + - (passed ? 'not ' : '') + - 'to be resolved to ' + j$.pp(expectedValue); + if (expected.length === 1) { + expected = expected[0]; } - return actualPromise.then( - function(actualValue) { - if (self.util.equals(actualValue, expectedValue, self.customEqualityTesters)) { - return { - pass: true, - message: prefix(true) + '.' - }; - } else { - return { - pass: false, - message: prefix(false) + ' but it was resolved to ' + j$.pp(actualValue) + '.' - }; - } - }, - function() { - return { - pass: false, - message: prefix(false) + ' but it was rejected.' - }; + this.addExpectationResult( + result.pass, + { + matcherName: name, + passed: result.pass, + message: message, + error: undefined, + errorForStack: errorForStack, + actual: this.actual, + expected: expected // TODO: this may need to be arrayified/sliced } ); }; - /** - * Expect a promise to be rejected with a value equal to the expected, using deep equality comparison. - * @function - * @async - * @name async-matchers#toBeRejectedWith - * @param {Object} expected - Value that the promise is expected to reject to - * @example - * await expectAsync(aPromise).toBeRejectedWith({prop: 'value'}); - * @example - * return expectAsync(aPromise).toBeRejectedWith({prop: 'value'}); - */ - AsyncExpectation.prototype.toBeRejectedWith = function(actualPromise, expectedValue) { - var self = this; + AsyncExpectation.prototype.buildMessage = j$.Expectation.prototype.buildMessage; - function prefix(passed) { - return 'Expected a promise ' + - (passed ? 'not ' : '') + - 'to be rejected with ' + j$.pp(expectedValue); + AsyncExpectation.prototype.instantiateMatcher = j$.Expectation.prototype.instantiateMatcher; + + AsyncExpectation.prototype.addFilter = j$.Expectation.prototype.addFilter; + + AsyncExpectation.addCoreMatchers = function(matchers) { + var prototype = AsyncExpectation.prototype; + for (var matcherName in matchers) { + var matcher = matchers[matcherName]; + prototype[matcherName] = wrapCompare(matcherName, matcher); } - - return actualPromise.then( - function() { - return { - pass: false, - message: prefix(false) + ' but it was resolved.' - }; - }, - function(actualValue) { - if (self.util.equals(actualValue, expectedValue, self.customEqualityTesters)) { - return { - pass: true, - message: prefix(true) + '.' - }; - } else { - return { - pass: false, - message: prefix(false) + ' but it was rejected with ' + j$.pp(actualValue) + '.' - }; - } - } - ); - }; - - AsyncExpectation.prototype.addFilter = function(filter) { - var result = Object.create(this); - result.filters = this.filters.addFilter(filter); - return result; }; AsyncExpectation.factory = function(options) { @@ -2351,7 +2245,7 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { var negatingFilter = { selectComparisonFunc: function(matcher) { function defaultNegativeCompare() { - return matcher.apply(this, arguments).then(function(result) { + return matcher.compare.apply(this, arguments).then(function(result) { result.pass = !result.pass; return result; }); @@ -3069,7 +2963,6 @@ getJasmineRequireObj().Expectation = function(j$) { expected = expected[0]; } - // TODO: how many of these params are needed? this.addExpectationResult( result.pass, { @@ -3358,6 +3251,146 @@ getJasmineRequireObj().GlobalErrors = function(j$) { return GlobalErrors; }; +getJasmineRequireObj().toBeRejected = function(j$) { + /** + * Expect a promise to be rejected. + * @function + * @async + * @name async-matchers#toBeRejected + * @example + * await expectAsync(aPromise).toBeRejected(); + * @example + * return expectAsync(aPromise).toBeRejected(); + */ + return function toBeResolved(util) { + return { + compare: function(actual) { + return actual.then( + function() { return {pass: false}; }, + function() { return {pass: true}; } + ); + } + }; + }; +}; + +getJasmineRequireObj().toBeRejectedWith = function(j$) { + /** + * Expect a promise to be rejected with a value equal to the expected, using deep equality comparison. + * @function + * @async + * @name async-matchers#toBeRejectedWith + * @param {Object} expected - Value that the promise is expected to be rejected with + * @example + * await expectAsync(aPromise).toBeRejectedWith({prop: 'value'}); + * @example + * return expectAsync(aPromise).toBeRejectedWith({prop: 'value'}); + */ + return function toBeRejectedWith(util, customEqualityTesters) { + return { + compare: function(actualPromise, expectedValue) { + function prefix(passed) { + return 'Expected a promise ' + + (passed ? 'not ' : '') + + 'to be rejected with ' + j$.pp(expectedValue); + } + + return actualPromise.then( + function() { + return { + pass: false, + message: prefix(false) + ' but it was resolved.' + }; + }, + function(actualValue) { + if (util.equals(actualValue, expectedValue, customEqualityTesters)) { + return { + pass: true, + message: prefix(true) + '.' + }; + } else { + return { + pass: false, + message: prefix(false) + ' but it was rejected with ' + j$.pp(actualValue) + '.' + }; + } + } + ); + } + }; + }; +}; + +getJasmineRequireObj().toBeResolved = function(j$) { + /** + * Expect a promise to be resolved. + * @function + * @async + * @name async-matchers#toBeResolved + * @example + * await expectAsync(aPromise).toBeResolved(); + * @example + * return expectAsync(aPromise).toBeResolved(); + */ + return function toBeResolved(util) { + return { + compare: function(actual) { + return actual.then( + function() { return {pass: true}; }, + function() { return {pass: false}; } + ); + } + }; + }; +}; + +getJasmineRequireObj().toBeResolvedTo = function(j$) { + /** + * Expect a promise to be resolved to a value equal to the expected, using deep equality comparison. + * @function + * @async + * @name async-matchers#toBeResolvedTo + * @param {Object} expected - Value that the promise is expected to resolve to + * @example + * await expectAsync(aPromise).toBeResolvedTo({prop: 'value'}); + * @example + * return expectAsync(aPromise).toBeResolvedTo({prop: 'value'}); + */ + return function toBeResolvedTo(util, customEqualityTesters) { + return { + compare: function(actualPromise, expectedValue) { + function prefix(passed) { + return 'Expected a promise ' + + (passed ? 'not ' : '') + + 'to be resolved to ' + j$.pp(expectedValue); + } + + return actualPromise.then( + function(actualValue) { + if (util.equals(actualValue, expectedValue, customEqualityTesters)) { + return { + pass: true, + message: prefix(true) + '.' + }; + } else { + return { + pass: false, + message: prefix(false) + ' but it was resolved to ' + j$.pp(actualValue) + '.' + }; + } + }, + function() { + return { + pass: false, + message: prefix(false) + ' but it was rejected.' + }; + } + ); + } + }; + }; +}; + getJasmineRequireObj().DiffBuilder = function(j$) { return function DiffBuilder() { var path = new j$.ObjectPath(), @@ -3948,6 +3981,23 @@ getJasmineRequireObj().ObjectPath = function(j$) { return ObjectPath; }; +getJasmineRequireObj().requireAsyncMatchers = function(jRequire, j$) { + var availableMatchers = [ + 'toBeResolved', + 'toBeRejected', + 'toBeResolvedTo', + 'toBeRejectedWith' + ], + matchers = {}; + + for (var i = 0; i < availableMatchers.length; i++) { + var name = availableMatchers[i]; + matchers[name] = jRequire[name](j$); + } + + return matchers; +}; + getJasmineRequireObj().toBe = function(j$) { /** * {@link expect} the actual value to be `===` to the expected value. diff --git a/spec/core/AsyncExpectationSpec.js b/spec/core/AsyncExpectationSpec.js index 69459534..3c260f13 100644 --- a/spec/core/AsyncExpectationSpec.js +++ b/spec/core/AsyncExpectationSpec.js @@ -1,4 +1,8 @@ describe('AsyncExpectation', function() { + beforeEach(function() { + jasmineUnderTest.AsyncExpectation.addCoreMatchers(jasmineUnderTest.asyncMatchers); + }); + describe('Factory', function() { it('throws an Error if promises are not available', function() { var thenable = {then: function() {}}, @@ -16,315 +20,6 @@ describe('AsyncExpectation', function() { }); }); - describe('#toBeResolved', function() { - it('passes if the actual is resolved', function() { - jasmine.getEnv().requirePromises(); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - actual = Promise.resolve(), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeResolved().then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(true, { - matcherName: 'toBeResolved', - passed: true, - message: '', - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - }); - - it('fails if the actual is rejected', function() { - jasmine.getEnv().requirePromises(); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - actual = Promise.reject('AsyncExpectationSpec rejection'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeResolved().then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(false, { - matcherName: 'toBeResolved', - passed: false, - message: 'Expected a promise to be resolved.', - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - }); - }); - - describe('#toBeRejected', function() { - it('passes if the actual is rejected', function() { - jasmine.getEnv().requirePromises(); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - actual = Promise.reject('AsyncExpectationSpec rejection'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeRejected().then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(true, { - matcherName: 'toBeRejected', - passed: true, - message: '', - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - }); - - it('fails if the actual is resolved', function() { - jasmine.getEnv().requirePromises(); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - actual = Promise.resolve(), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeRejected().then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(false, { - matcherName: 'toBeRejected', - passed: false, - message: 'Expected a promise to be rejected.', - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - }); - }); - - describe('#toBeRejectedWith', function () { - it('should return true if the promise is rejected with the expected value', function () { - jasmine.getEnv().requirePromises(); - - var actual = Promise.reject({error: 'PEBCAK'}); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeRejectedWith({error: 'PEBCAK'}).then(function () { - expect(addExpectationResult).toHaveBeenCalledWith(true, { - matcherName: 'toBeRejectedWith', - passed: true, - message: '', - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - - }); - - it('should fail if the promise resolves', function () { - jasmine.getEnv().requirePromises(); - - var actual = Promise.resolve('AsyncExpectation error'); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeRejectedWith('').then(function () { - expect(addExpectationResult).toHaveBeenCalledWith(false, { - matcherName: 'toBeRejectedWith', - passed: false, - message: "Expected a promise to be rejected with '' but it was resolved.", - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - }); - - it('should fail if the promise is rejected with a different value', function () { - jasmine.getEnv().requirePromises(); - - var actual = Promise.reject('A Bad Apple'); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeRejectedWith('Some Cool Thing').then(function () { - expect(addExpectationResult).toHaveBeenCalledWith(false, { - matcherName: 'toBeRejectedWith', - passed: false, - message: "Expected a promise to be rejected with 'Some Cool Thing' but it was rejected with 'A Bad Apple'.", - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - }); - - it('should build its error correctly when negated', function () { - jasmine.getEnv().requirePromises(); - - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = jasmineUnderTest.AsyncExpectation.factory({ - util: jasmineUnderTest.matchersUtil, - actual: Promise.reject(true), - addExpectationResult: addExpectationResult - }); - - return expectation.not.toBeRejectedWith(true).then(function () { - expect(addExpectationResult).toHaveBeenCalledWith(false, - jasmine.objectContaining({ - passed: false, - message: 'Expected a promise not to be rejected with true.' - }) - ); - }); - }); - - it('should support custom equality testers', function () { - jasmine.getEnv().requirePromises(); - - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - customEqualityTesters: [function() { return true; }], - actual: Promise.reject('actual'), - addExpectationResult: addExpectationResult - }); - - return expectation.toBeRejectedWith('expected').then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(true, - jasmine.objectContaining({passed: true})); - }); - }); - }); - - describe('#toBeResolvedTo', function() { - it('passes if the promise is resolved to the expected value', function() { - jasmine.getEnv().requirePromises(); - - var actual = Promise.resolve({foo: 42}); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeResolvedTo({foo: 42}).then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(true, { - matcherName: 'toBeResolvedTo', - passed: true, - message: '', - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - }); - - it('fails if the promise is rejected', function() { - jasmine.getEnv().requirePromises(); - - var actual = Promise.reject('AsyncExpectationSpec error'); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeResolvedTo('').then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(false, { - matcherName: 'toBeResolvedTo', - passed: false, - message: "Expected a promise to be resolved to '' but it was rejected.", - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - }); - - it('fails if the promise is resolved to a different value', function() { - jasmine.getEnv().requirePromises(); - - var actual = Promise.resolve({foo: 17}); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - actual: actual, - addExpectationResult: addExpectationResult - }); - - return expectation.toBeResolvedTo({foo: 42}).then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(false, { - matcherName: 'toBeResolvedTo', - passed: false, - message: 'Expected a promise to be resolved to Object({ foo: 42 }) but it was resolved to Object({ foo: 17 }).', - error: undefined, - errorForStack: jasmine.any(Error), - actual: actual - }); - }); - }); - - it('builds its message correctly when negated', function() { - jasmine.getEnv().requirePromises(); - - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = jasmineUnderTest.AsyncExpectation.factory({ - util: jasmineUnderTest.matchersUtil, - actual: Promise.resolve(true), - addExpectationResult: addExpectationResult - }); - - return expectation.not.toBeResolvedTo(true).then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(false, - jasmine.objectContaining({ - passed: false, - message: 'Expected a promise not to be resolved to true.' - }) - ); - }); - }); - - it('supports custom equality testers', function() { - jasmine.getEnv().requirePromises(); - - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - expectation = new jasmineUnderTest.AsyncExpectation({ - util: jasmineUnderTest.matchersUtil, - customEqualityTesters: [function() { return true; }], - actual: Promise.resolve('actual'), - addExpectationResult: addExpectationResult - }); - - return expectation.toBeResolvedTo('expected').then(function() { - expect(addExpectationResult).toHaveBeenCalledWith(true, - jasmine.objectContaining({passed: true})); - }); - }); - }); - describe('#not', function() { it('converts a pass to a fail', function() { jasmine.getEnv().requirePromises(); @@ -394,16 +89,12 @@ describe('AsyncExpectation', function() { it("prepends the context to the generated failure message", function() { jasmine.getEnv().requirePromises(); - spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved') - .and.returnValue(Promise.resolve({pass: false})); - var util = { buildFailureMessage: function() { return 'failure message'; } }, addExpectationResult = jasmine.createSpy('addExpectationResult'), - actual = dummyPromise(), expectation = jasmineUnderTest.AsyncExpectation.factory({ - actual: actual, + actual: Promise.reject('rejected'), addExpectationResult: addExpectationResult, util: util }); @@ -421,41 +112,35 @@ describe('AsyncExpectation', function() { it("prepends the context to a custom failure message", function() { jasmine.getEnv().requirePromises(); - spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved') - .and.returnValue(Promise.resolve({pass: false, message: 'msg'})); - var util = { buildFailureMessage: function() { return 'failure message'; } }, addExpectationResult = jasmine.createSpy('addExpectationResult'), - actual = dummyPromise(), expectation = jasmineUnderTest.AsyncExpectation.factory({ - actual: actual, + actual: Promise.reject('b'), addExpectationResult: addExpectationResult, util: util }); - return expectation.withContext('Some context').toBeResolved() + return expectation.withContext('Some context').toBeResolvedTo('a') .then( function() { expect(addExpectationResult).toHaveBeenCalledWith(false, jasmine.objectContaining({ - message: 'Some context: msg' + message: "Some context: Expected a promise to be resolved to 'a' but it was rejected." })); }); }); it("prepends the context to a custom failure message from a function", function() { + pending('should actually work, but no custom matchers for async yet'); jasmine.getEnv().requirePromises(); - spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved') - .and.returnValue(Promise.resolve({pass: false, message: function() { return 'msg'; } })); - var util = { buildFailureMessage: function() { return 'failure message'; } }, addExpectationResult = jasmine.createSpy('addExpectationResult'), - actual = dummyPromise(), + actual = Promise.reject(), expectation = jasmineUnderTest.AsyncExpectation.factory({ actual: actual, addExpectationResult: addExpectationResult, @@ -475,11 +160,8 @@ describe('AsyncExpectation', function() { it("works with #not", function() { jasmine.getEnv().requirePromises(); - spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved') - .and.returnValue(Promise.resolve({pass: true})); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - actual = dummyPromise(), + actual = Promise.resolve(), expectation = jasmineUnderTest.AsyncExpectation.factory({ actual: actual, addExpectationResult: addExpectationResult, @@ -499,23 +181,20 @@ describe('AsyncExpectation', function() { it("works with #not and a custom message", function() { jasmine.getEnv().requirePromises(); - spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved') - .and.returnValue(Promise.resolve({pass: true, message: 'msg'})); - var addExpectationResult = jasmine.createSpy('addExpectationResult'), - actual = dummyPromise(), + actual = Promise.resolve('a'), expectation = jasmineUnderTest.AsyncExpectation.factory({ actual: actual, addExpectationResult: addExpectationResult, util: jasmineUnderTest.matchersUtil }); - return expectation.withContext('Some context').not.toBeResolved() + return expectation.withContext('Some context').not.toBeResolvedTo('a') .then( function() { expect(addExpectationResult).toHaveBeenCalledWith(false, jasmine.objectContaining({ - message: 'Some context: msg' + message: "Some context: Expected a promise not to be resolved to 'a'." })); }); }); diff --git a/spec/core/matchers/async/toBeRejectedSpec.js b/spec/core/matchers/async/toBeRejectedSpec.js new file mode 100644 index 00000000..ff718e49 --- /dev/null +++ b/spec/core/matchers/async/toBeRejectedSpec.js @@ -0,0 +1,23 @@ +describe('toBeRejected', function() { + it('passes if the actual is rejected', function() { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeRejected(jasmineUnderTest.matchersUtil), + actual = Promise.reject('AsyncExpectationSpec rejection'); + + return matcher.compare(actual).then(function(result) { + expect(result).toEqual(jasmine.objectContaining({pass: true})); + }); + }); + + it('fails if the actual is resolved', function() { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeRejected(jasmineUnderTest.matchersUtil), + actual = Promise.resolve(); + + return matcher.compare(actual).then(function(result) { + expect(result).toEqual(jasmine.objectContaining({pass: false})); + }); + }); +}); diff --git a/spec/core/matchers/async/toBeRejectedWithSpec.js b/spec/core/matchers/async/toBeRejectedWithSpec.js new file mode 100644 index 00000000..899d8a79 --- /dev/null +++ b/spec/core/matchers/async/toBeRejectedWithSpec.js @@ -0,0 +1,63 @@ +describe('#toBeRejectedWith', function () { + it('should return true if the promise is rejected with the expected value', function () { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil), + actual = Promise.reject({error: 'PEBCAK'}); + + return matcher.compare(actual, {error: 'PEBCAK'}).then(function (result) { + expect(result).toEqual(jasmine.objectContaining({ pass: true })); + }); + }); + + it('should fail if the promise resolves', function () { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil), + actual = Promise.resolve(); + + return matcher.compare(actual, '').then(function (result) { + expect(result).toEqual(jasmine.objectContaining({ pass: false })); + }); + }); + + it('should fail if the promise is rejected with a different value', function () { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil), + actual = Promise.reject('A Bad Apple'); + + return matcher.compare(actual, 'Some Cool Thing').then(function (result) { + expect(result).toEqual(jasmine.objectContaining({ + pass: false, + message: "Expected a promise to be rejected with 'Some Cool Thing' but it was rejected with 'A Bad Apple'.", + })); + }); + }); + + it('should build its error correctly when negated', function () { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil), + actual = Promise.reject(true); + + return matcher.compare(actual, true).then(function (result) { + expect(result).toEqual(jasmine.objectContaining({ + pass: true, + message: 'Expected a promise not to be rejected with true.' + })); + }); + }); + + it('should support custom equality testers', function () { + jasmine.getEnv().requirePromises(); + + var customEqualityTesters = [function() { return true; }], + matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil, customEqualityTesters), + actual = Promise.reject('actual'); + + return matcher.compare(actual, 'expected').then(function(result) { + expect(result).toEqual(jasmine.objectContaining({pass: true})); + }); + }); +}); diff --git a/spec/core/matchers/async/toBeResolvedSpec.js b/spec/core/matchers/async/toBeResolvedSpec.js new file mode 100644 index 00000000..ba66ab67 --- /dev/null +++ b/spec/core/matchers/async/toBeResolvedSpec.js @@ -0,0 +1,23 @@ +describe('toBeResolved', function() { + it('passes if the actual is resolved', function() { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeResolved(jasmineUnderTest.matchersUtil), + actual = Promise.resolve(); + + return matcher.compare(actual).then(function(result) { + expect(result).toEqual(jasmine.objectContaining({pass: true})); + }); + }); + + it('fails if the actual is rejected', function() { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeResolved(jasmineUnderTest.matchersUtil), + actual = Promise.reject('AsyncExpectationSpec rejection'); + + return matcher.compare(actual).then(function(result) { + expect(result).toEqual(jasmine.objectContaining({pass: false})); + }); + }); +}); diff --git a/spec/core/matchers/async/toBeResolvedToSpec.js b/spec/core/matchers/async/toBeResolvedToSpec.js new file mode 100644 index 00000000..e8baaf7d --- /dev/null +++ b/spec/core/matchers/async/toBeResolvedToSpec.js @@ -0,0 +1,66 @@ +describe('#toBeResolvedTo', function() { + it('passes if the promise is resolved to the expected value', function() { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil), + actual = Promise.resolve({foo: 42}); + + return matcher.compare(actual, {foo: 42}).then(function(result) { + expect(result).toEqual(jasmine.objectContaining({pass: true})); + }); + }); + + it('fails if the promise is rejected', function() { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil), + actual = Promise.reject('AsyncExpectationSpec error'); + + return matcher.compare(actual, '').then(function(result) { + expect(result).toEqual(jasmine.objectContaining({ + pass: false, + message: "Expected a promise to be resolved to '' but it was rejected.", + })); + }); + }); + + it('fails if the promise is resolved to a different value', function() { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil), + actual = Promise.resolve({foo: 17}); + + return matcher.compare(actual, {foo: 42}).then(function(result) { + expect(result).toEqual(jasmine.objectContaining({ + pass: false, + message: 'Expected a promise to be resolved to Object({ foo: 42 }) but it was resolved to Object({ foo: 17 }).', + })); + }); + }); + + it('builds its message correctly when negated', function() { + jasmine.getEnv().requirePromises(); + + var matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil), + actual = Promise.resolve(true); + + return matcher.compare(actual, true).then(function(result) { + expect(result).toEqual(jasmine.objectContaining({ + pass: true, + message: 'Expected a promise not to be resolved to true.' + })); + }); + }); + + it('supports custom equality testers', function() { + jasmine.getEnv().requirePromises(); + + var customEqualityTesters = [function() { return true; }], + matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil, customEqualityTesters), + actual = Promise.resolve('actual'); + + return matcher.compare(actual, 'expected').then(function(result) { + expect(result).toEqual(jasmine.objectContaining({pass: true})); + }); + }); +}); diff --git a/src/core/AsyncExpectation.js b/src/core/AsyncExpectation.js index 50d9dc82..c88f699b 100644 --- a/src/core/AsyncExpectation.js +++ b/src/core/AsyncExpectation.js @@ -22,15 +22,14 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { if (!j$.isPromiseLike(this.actual)) { throw new Error('Expected expectAsync to be called with a promise.'); } - - ['toBeResolved', 'toBeRejected', 'toBeResolvedTo', 'toBeRejectedWith'].forEach(wrapCompare.bind(this)); } - function wrapCompare(name) { - var matcher = this[name]; - this[name] = function() { + function wrapCompare(name, matcherFactory) { + return function() { var self = this; - var args = Array.prototype.slice.call(arguments); + var args = Array.prototype.slice.call(arguments), + expected = args.slice(0); + args.unshift(this.actual); // Capture the call stack here, before we go async, so that it will @@ -38,155 +37,48 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { // of Jasmine. var errorForStack = j$.util.errorWithStack(); - var matcherCompare = this.instantiateMatcher(matcher); + var matcherCompare = this.instantiateMatcher(matcherFactory); return matcherCompare.apply(self, args).then(function(result) { - var message; - args[0] = promiseForMessage; - message = j$.Expectation.prototype.buildMessage.call(self, result, name, args); - - self.addExpectationResult(result.pass, { - matcherName: name, - passed: result.pass, - message: message, - error: undefined, - errorForStack: errorForStack, - actual: self.actual - }); + self.processResult(result, name, expected, args, errorForStack); }); }; } - AsyncExpectation.prototype.instantiateMatcher = function(matcher) { - var comparisonFunc = this.filters.selectComparisonFunc(matcher); - return comparisonFunc || matcher; - }; + AsyncExpectation.prototype.processResult = function(result, name, expected, args, errorForStack) { + var message = this.buildMessage(result, name, args); - /** - * Expect a promise to be resolved. - * @function - * @async - * @name async-matchers#toBeResolved - * @example - * await expectAsync(aPromise).toBeResolved(); - * @example - * return expectAsync(aPromise).toBeResolved(); - */ - AsyncExpectation.prototype.toBeResolved = function(actual) { - return actual.then( - function() { return {pass: true}; }, - function() { return {pass: false}; } - ); - }; - - /** - * Expect a promise to be rejected. - * @function - * @async - * @name async-matchers#toBeRejected - * @example - * await expectAsync(aPromise).toBeRejected(); - * @example - * return expectAsync(aPromise).toBeRejected(); - */ - AsyncExpectation.prototype.toBeRejected = function(actual) { - return actual.then( - function() { return {pass: false}; }, - function() { return {pass: true}; } - ); - }; - - /** - * Expect a promise to be resolved to a value equal to the expected, using deep equality comparison. - * @function - * @async - * @name async-matchers#toBeResolvedTo - * @param {Object} expected - Value that the promise is expected to resolve to - * @example - * await expectAsync(aPromise).toBeResolvedTo({prop: 'value'}); - * @example - * return expectAsync(aPromise).toBeResolvedTo({prop: 'value'}); - */ - AsyncExpectation.prototype.toBeResolvedTo = function(actualPromise, expectedValue) { - var self = this; - - function prefix(passed) { - return 'Expected a promise ' + - (passed ? 'not ' : '') + - 'to be resolved to ' + j$.pp(expectedValue); + if (expected.length === 1) { + expected = expected[0]; } - return actualPromise.then( - function(actualValue) { - if (self.util.equals(actualValue, expectedValue, self.customEqualityTesters)) { - return { - pass: true, - message: prefix(true) + '.' - }; - } else { - return { - pass: false, - message: prefix(false) + ' but it was resolved to ' + j$.pp(actualValue) + '.' - }; - } - }, - function() { - return { - pass: false, - message: prefix(false) + ' but it was rejected.' - }; + this.addExpectationResult( + result.pass, + { + matcherName: name, + passed: result.pass, + message: message, + error: undefined, + errorForStack: errorForStack, + actual: this.actual, + expected: expected // TODO: this may need to be arrayified/sliced } ); }; - /** - * Expect a promise to be rejected with a value equal to the expected, using deep equality comparison. - * @function - * @async - * @name async-matchers#toBeRejectedWith - * @param {Object} expected - Value that the promise is expected to reject to - * @example - * await expectAsync(aPromise).toBeRejectedWith({prop: 'value'}); - * @example - * return expectAsync(aPromise).toBeRejectedWith({prop: 'value'}); - */ - AsyncExpectation.prototype.toBeRejectedWith = function(actualPromise, expectedValue) { - var self = this; + AsyncExpectation.prototype.buildMessage = j$.Expectation.prototype.buildMessage; - function prefix(passed) { - return 'Expected a promise ' + - (passed ? 'not ' : '') + - 'to be rejected with ' + j$.pp(expectedValue); + AsyncExpectation.prototype.instantiateMatcher = j$.Expectation.prototype.instantiateMatcher; + + AsyncExpectation.prototype.addFilter = j$.Expectation.prototype.addFilter; + + AsyncExpectation.addCoreMatchers = function(matchers) { + var prototype = AsyncExpectation.prototype; + for (var matcherName in matchers) { + var matcher = matchers[matcherName]; + prototype[matcherName] = wrapCompare(matcherName, matcher); } - - return actualPromise.then( - function() { - return { - pass: false, - message: prefix(false) + ' but it was resolved.' - }; - }, - function(actualValue) { - if (self.util.equals(actualValue, expectedValue, self.customEqualityTesters)) { - return { - pass: true, - message: prefix(true) + '.' - }; - } else { - return { - pass: false, - message: prefix(false) + ' but it was rejected with ' + j$.pp(actualValue) + '.' - }; - } - } - ); - }; - - AsyncExpectation.prototype.addFilter = function(filter) { - var result = Object.create(this); - result.filters = this.filters.addFilter(filter); - return result; }; AsyncExpectation.factory = function(options) { @@ -205,7 +97,7 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { var negatingFilter = { selectComparisonFunc: function(matcher) { function defaultNegativeCompare() { - return matcher.apply(this, arguments).then(function(result) { + return matcher.compare.apply(this, arguments).then(function(result) { result.pass = !result.pass; return result; }); diff --git a/src/core/Env.js b/src/core/Env.js index 19105525..a5f6c4df 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -193,6 +193,7 @@ getJasmineRequireObj().Env = function(j$) { }; j$.Expectation.addCoreMatchers(j$.matchers); + j$.AsyncExpectation.addCoreMatchers(j$.asyncMatchers); var nextSpecId = 0; var getNextSpecId = function() { diff --git a/src/core/Expectation.js b/src/core/Expectation.js index 5367ab24..e6b6fc70 100644 --- a/src/core/Expectation.js +++ b/src/core/Expectation.js @@ -43,7 +43,6 @@ getJasmineRequireObj().Expectation = function(j$) { expected = expected[0]; } - // TODO: how many of these params are needed? this.addExpectationResult( result.pass, { diff --git a/src/core/matchers/async/toBeRejected.js b/src/core/matchers/async/toBeRejected.js new file mode 100644 index 00000000..8451e8df --- /dev/null +++ b/src/core/matchers/async/toBeRejected.js @@ -0,0 +1,22 @@ +getJasmineRequireObj().toBeRejected = function(j$) { + /** + * Expect a promise to be rejected. + * @function + * @async + * @name async-matchers#toBeRejected + * @example + * await expectAsync(aPromise).toBeRejected(); + * @example + * return expectAsync(aPromise).toBeRejected(); + */ + return function toBeResolved(util) { + return { + compare: function(actual) { + return actual.then( + function() { return {pass: false}; }, + function() { return {pass: true}; } + ); + } + }; + }; +}; diff --git a/src/core/matchers/async/toBeRejectedWith.js b/src/core/matchers/async/toBeRejectedWith.js new file mode 100644 index 00000000..3e302de2 --- /dev/null +++ b/src/core/matchers/async/toBeRejectedWith.js @@ -0,0 +1,46 @@ +getJasmineRequireObj().toBeRejectedWith = function(j$) { + /** + * Expect a promise to be rejected with a value equal to the expected, using deep equality comparison. + * @function + * @async + * @name async-matchers#toBeRejectedWith + * @param {Object} expected - Value that the promise is expected to be rejected with + * @example + * await expectAsync(aPromise).toBeRejectedWith({prop: 'value'}); + * @example + * return expectAsync(aPromise).toBeRejectedWith({prop: 'value'}); + */ + return function toBeRejectedWith(util, customEqualityTesters) { + return { + compare: function(actualPromise, expectedValue) { + function prefix(passed) { + return 'Expected a promise ' + + (passed ? 'not ' : '') + + 'to be rejected with ' + j$.pp(expectedValue); + } + + return actualPromise.then( + function() { + return { + pass: false, + message: prefix(false) + ' but it was resolved.' + }; + }, + function(actualValue) { + if (util.equals(actualValue, expectedValue, customEqualityTesters)) { + return { + pass: true, + message: prefix(true) + '.' + }; + } else { + return { + pass: false, + message: prefix(false) + ' but it was rejected with ' + j$.pp(actualValue) + '.' + }; + } + } + ); + } + }; + }; +}; diff --git a/src/core/matchers/async/toBeResolved.js b/src/core/matchers/async/toBeResolved.js new file mode 100644 index 00000000..a26f265d --- /dev/null +++ b/src/core/matchers/async/toBeResolved.js @@ -0,0 +1,22 @@ +getJasmineRequireObj().toBeResolved = function(j$) { + /** + * Expect a promise to be resolved. + * @function + * @async + * @name async-matchers#toBeResolved + * @example + * await expectAsync(aPromise).toBeResolved(); + * @example + * return expectAsync(aPromise).toBeResolved(); + */ + return function toBeResolved(util) { + return { + compare: function(actual) { + return actual.then( + function() { return {pass: true}; }, + function() { return {pass: false}; } + ); + } + }; + }; +}; diff --git a/src/core/matchers/async/toBeResolvedTo.js b/src/core/matchers/async/toBeResolvedTo.js new file mode 100644 index 00000000..f178b673 --- /dev/null +++ b/src/core/matchers/async/toBeResolvedTo.js @@ -0,0 +1,46 @@ +getJasmineRequireObj().toBeResolvedTo = function(j$) { + /** + * Expect a promise to be resolved to a value equal to the expected, using deep equality comparison. + * @function + * @async + * @name async-matchers#toBeResolvedTo + * @param {Object} expected - Value that the promise is expected to resolve to + * @example + * await expectAsync(aPromise).toBeResolvedTo({prop: 'value'}); + * @example + * return expectAsync(aPromise).toBeResolvedTo({prop: 'value'}); + */ + return function toBeResolvedTo(util, customEqualityTesters) { + return { + compare: function(actualPromise, expectedValue) { + function prefix(passed) { + return 'Expected a promise ' + + (passed ? 'not ' : '') + + 'to be resolved to ' + j$.pp(expectedValue); + } + + return actualPromise.then( + function(actualValue) { + if (util.equals(actualValue, expectedValue, customEqualityTesters)) { + return { + pass: true, + message: prefix(true) + '.' + }; + } else { + return { + pass: false, + message: prefix(false) + ' but it was resolved to ' + j$.pp(actualValue) + '.' + }; + } + }, + function() { + return { + pass: false, + message: prefix(false) + ' but it was rejected.' + }; + } + ); + } + }; + }; +}; diff --git a/src/core/matchers/requireAsyncMatchers.js b/src/core/matchers/requireAsyncMatchers.js new file mode 100644 index 00000000..1474c22d --- /dev/null +++ b/src/core/matchers/requireAsyncMatchers.js @@ -0,0 +1,16 @@ +getJasmineRequireObj().requireAsyncMatchers = function(jRequire, j$) { + var availableMatchers = [ + 'toBeResolved', + 'toBeRejected', + 'toBeResolvedTo', + 'toBeRejectedWith' + ], + matchers = {}; + + for (var i = 0; i < availableMatchers.length; i++) { + var name = availableMatchers[i]; + matchers[name] = jRequire[name](j$); + } + + return matchers; +}; diff --git a/src/core/requireCore.js b/src/core/requireCore.js index 6ccacf44..181c2c32 100644 --- a/src/core/requireCore.js +++ b/src/core/requireCore.js @@ -72,6 +72,7 @@ var getJasmineRequireObj = (function (jasmineGlobal) { j$.NotEmpty = jRequire.NotEmpty(j$); j$.matchers = jRequire.requireMatchers(jRequire, j$); + j$.asyncMatchers = jRequire.requireAsyncMatchers(jRequire, j$); return j$; };