diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index acbf610f..4832011e 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -2339,6 +2339,12 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { var expect = new AsyncExpectation(options); expect.not = expect.addFilter(negatingFilter); + expect.withContext = function(message) { + var result = this.addFilter(new ContextAddingFilter(message)); + result.not = result.addFilter(negatingFilter); + return result; + }; + return expect; }; @@ -2370,6 +2376,14 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { }; + function ContextAddingFilter(message) { + this.message = message; + } + + ContextAddingFilter.prototype.modifyFailureMessage = function(msg) { + return this.message + ': ' + msg; + }; + return AsyncExpectation; }; diff --git a/spec/core/AsyncExpectationSpec.js b/spec/core/AsyncExpectationSpec.js index 7aefa191..69459534 100644 --- a/spec/core/AsyncExpectationSpec.js +++ b/spec/core/AsyncExpectationSpec.js @@ -390,6 +390,137 @@ describe('AsyncExpectation', function() { ); }); + describe('#withContext', 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, + addExpectationResult: addExpectationResult, + util: util + }); + + return expectation.withContext('Some context').toBeResolved() + .then( + function() { + expect(addExpectationResult).toHaveBeenCalledWith(false, + jasmine.objectContaining({ + message: 'Some context: failure message' + })); + }); + }); + + 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, + addExpectationResult: addExpectationResult, + util: util + }); + + return expectation.withContext('Some context').toBeResolved() + .then( + function() { + expect(addExpectationResult).toHaveBeenCalledWith(false, + jasmine.objectContaining({ + message: 'Some context: msg' + })); + }); + }); + + it("prepends the context to a custom failure message from a function", function() { + 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(), + expectation = jasmineUnderTest.AsyncExpectation.factory({ + actual: actual, + addExpectationResult: addExpectationResult, + util: util + }); + + return expectation.withContext('Some context').toBeResolved() + .then( + function() { + expect(addExpectationResult).toHaveBeenCalledWith(false, + jasmine.objectContaining({ + message: 'Some context: msg' + })); + }); + }); + + 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(), + expectation = jasmineUnderTest.AsyncExpectation.factory({ + actual: actual, + addExpectationResult: addExpectationResult, + util: jasmineUnderTest.matchersUtil + }); + + return expectation.withContext('Some context').not.toBeResolved() + .then( + function() { + expect(addExpectationResult).toHaveBeenCalledWith(false, + jasmine.objectContaining({ + message: 'Some context: Expected a promise not to be resolved.' + })); + }); + }); + + 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(), + expectation = jasmineUnderTest.AsyncExpectation.factory({ + actual: actual, + addExpectationResult: addExpectationResult, + util: jasmineUnderTest.matchersUtil + }); + + return expectation.withContext('Some context').not.toBeResolved() + .then( + function() { + expect(addExpectationResult).toHaveBeenCalledWith(false, + jasmine.objectContaining({ + message: 'Some context: msg' + })); + }); + }); + }); + function dummyPromise() { return new Promise(function(resolve, reject) { }); diff --git a/src/core/AsyncExpectation.js b/src/core/AsyncExpectation.js index e2d7d47f..50d9dc82 100644 --- a/src/core/AsyncExpectation.js +++ b/src/core/AsyncExpectation.js @@ -193,6 +193,12 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { var expect = new AsyncExpectation(options); expect.not = expect.addFilter(negatingFilter); + expect.withContext = function(message) { + var result = this.addFilter(new ContextAddingFilter(message)); + result.not = result.addFilter(negatingFilter); + return result; + }; + return expect; }; @@ -224,5 +230,13 @@ getJasmineRequireObj().AsyncExpectation = function(j$) { }; + function ContextAddingFilter(message) { + this.message = message; + } + + ContextAddingFilter.prototype.modifyFailureMessage = function(msg) { + return this.message + ': ' + msg; + }; + return AsyncExpectation; };