Pull async matchers out to their own functions
- Makes AsyncExpectation closer to Expectation
This commit is contained in:
@@ -94,6 +94,7 @@ var getJasmineRequireObj = (function (jasmineGlobal) {
|
|||||||
j$.NotEmpty = jRequire.NotEmpty(j$);
|
j$.NotEmpty = jRequire.NotEmpty(j$);
|
||||||
|
|
||||||
j$.matchers = jRequire.requireMatchers(jRequire, j$);
|
j$.matchers = jRequire.requireMatchers(jRequire, j$);
|
||||||
|
j$.asyncMatchers = jRequire.requireAsyncMatchers(jRequire, j$);
|
||||||
|
|
||||||
return j$;
|
return j$;
|
||||||
};
|
};
|
||||||
@@ -969,6 +970,7 @@ getJasmineRequireObj().Env = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
j$.Expectation.addCoreMatchers(j$.matchers);
|
j$.Expectation.addCoreMatchers(j$.matchers);
|
||||||
|
j$.AsyncExpectation.addCoreMatchers(j$.asyncMatchers);
|
||||||
|
|
||||||
var nextSpecId = 0;
|
var nextSpecId = 0;
|
||||||
var getNextSpecId = function() {
|
var getNextSpecId = function() {
|
||||||
@@ -2168,15 +2170,14 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
if (!j$.isPromiseLike(this.actual)) {
|
if (!j$.isPromiseLike(this.actual)) {
|
||||||
throw new Error('Expected expectAsync to be called with a promise.');
|
throw new Error('Expected expectAsync to be called with a promise.');
|
||||||
}
|
}
|
||||||
|
|
||||||
['toBeResolved', 'toBeRejected', 'toBeResolvedTo', 'toBeRejectedWith'].forEach(wrapCompare.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapCompare(name) {
|
function wrapCompare(name, matcherFactory) {
|
||||||
var matcher = this[name];
|
return function() {
|
||||||
this[name] = function() {
|
|
||||||
var self = this;
|
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);
|
args.unshift(this.actual);
|
||||||
|
|
||||||
// Capture the call stack here, before we go async, so that it will
|
// Capture the call stack here, before we go async, so that it will
|
||||||
@@ -2184,155 +2185,48 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
// of Jasmine.
|
// of Jasmine.
|
||||||
var errorForStack = j$.util.errorWithStack();
|
var errorForStack = j$.util.errorWithStack();
|
||||||
|
|
||||||
var matcherCompare = this.instantiateMatcher(matcher);
|
var matcherCompare = this.instantiateMatcher(matcherFactory);
|
||||||
|
|
||||||
return matcherCompare.apply(self, args).then(function(result) {
|
return matcherCompare.apply(self, args).then(function(result) {
|
||||||
var message;
|
|
||||||
|
|
||||||
args[0] = promiseForMessage;
|
args[0] = promiseForMessage;
|
||||||
message = j$.Expectation.prototype.buildMessage.call(self, result, name, args);
|
self.processResult(result, name, expected, args, errorForStack);
|
||||||
|
|
||||||
self.addExpectationResult(result.pass, {
|
|
||||||
matcherName: name,
|
|
||||||
passed: result.pass,
|
|
||||||
message: message,
|
|
||||||
error: undefined,
|
|
||||||
errorForStack: errorForStack,
|
|
||||||
actual: self.actual
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
AsyncExpectation.prototype.instantiateMatcher = function(matcher) {
|
AsyncExpectation.prototype.processResult = function(result, name, expected, args, errorForStack) {
|
||||||
var comparisonFunc = this.filters.selectComparisonFunc(matcher);
|
var message = this.buildMessage(result, name, args);
|
||||||
return comparisonFunc || matcher;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
if (expected.length === 1) {
|
||||||
* Expect a promise to be resolved.
|
expected = expected[0];
|
||||||
* @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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return actualPromise.then(
|
this.addExpectationResult(
|
||||||
function(actualValue) {
|
result.pass,
|
||||||
if (self.util.equals(actualValue, expectedValue, self.customEqualityTesters)) {
|
{
|
||||||
return {
|
matcherName: name,
|
||||||
pass: true,
|
passed: result.pass,
|
||||||
message: prefix(true) + '.'
|
message: message,
|
||||||
};
|
error: undefined,
|
||||||
} else {
|
errorForStack: errorForStack,
|
||||||
return {
|
actual: this.actual,
|
||||||
pass: false,
|
expected: expected // TODO: this may need to be arrayified/sliced
|
||||||
message: prefix(false) + ' but it was resolved to ' + j$.pp(actualValue) + '.'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
},
|
|
||||||
function() {
|
|
||||||
return {
|
|
||||||
pass: false,
|
|
||||||
message: prefix(false) + ' but it was rejected.'
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
AsyncExpectation.prototype.buildMessage = j$.Expectation.prototype.buildMessage;
|
||||||
* 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;
|
|
||||||
|
|
||||||
function prefix(passed) {
|
AsyncExpectation.prototype.instantiateMatcher = j$.Expectation.prototype.instantiateMatcher;
|
||||||
return 'Expected a promise ' +
|
|
||||||
(passed ? 'not ' : '') +
|
AsyncExpectation.prototype.addFilter = j$.Expectation.prototype.addFilter;
|
||||||
'to be rejected with ' + j$.pp(expectedValue);
|
|
||||||
|
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) {
|
AsyncExpectation.factory = function(options) {
|
||||||
@@ -2351,7 +2245,7 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
var negatingFilter = {
|
var negatingFilter = {
|
||||||
selectComparisonFunc: function(matcher) {
|
selectComparisonFunc: function(matcher) {
|
||||||
function defaultNegativeCompare() {
|
function defaultNegativeCompare() {
|
||||||
return matcher.apply(this, arguments).then(function(result) {
|
return matcher.compare.apply(this, arguments).then(function(result) {
|
||||||
result.pass = !result.pass;
|
result.pass = !result.pass;
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
@@ -3069,7 +2963,6 @@ getJasmineRequireObj().Expectation = function(j$) {
|
|||||||
expected = expected[0];
|
expected = expected[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: how many of these params are needed?
|
|
||||||
this.addExpectationResult(
|
this.addExpectationResult(
|
||||||
result.pass,
|
result.pass,
|
||||||
{
|
{
|
||||||
@@ -3358,6 +3251,146 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
|
|||||||
return GlobalErrors;
|
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$) {
|
getJasmineRequireObj().DiffBuilder = function(j$) {
|
||||||
return function DiffBuilder() {
|
return function DiffBuilder() {
|
||||||
var path = new j$.ObjectPath(),
|
var path = new j$.ObjectPath(),
|
||||||
@@ -3948,6 +3981,23 @@ getJasmineRequireObj().ObjectPath = function(j$) {
|
|||||||
return ObjectPath;
|
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$) {
|
getJasmineRequireObj().toBe = function(j$) {
|
||||||
/**
|
/**
|
||||||
* {@link expect} the actual value to be `===` to the expected value.
|
* {@link expect} the actual value to be `===` to the expected value.
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
describe('AsyncExpectation', function() {
|
describe('AsyncExpectation', function() {
|
||||||
|
beforeEach(function() {
|
||||||
|
jasmineUnderTest.AsyncExpectation.addCoreMatchers(jasmineUnderTest.asyncMatchers);
|
||||||
|
});
|
||||||
|
|
||||||
describe('Factory', function() {
|
describe('Factory', function() {
|
||||||
it('throws an Error if promises are not available', function() {
|
it('throws an Error if promises are not available', function() {
|
||||||
var thenable = {then: 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() {
|
describe('#not', function() {
|
||||||
it('converts a pass to a fail', function() {
|
it('converts a pass to a fail', function() {
|
||||||
jasmine.getEnv().requirePromises();
|
jasmine.getEnv().requirePromises();
|
||||||
@@ -394,16 +89,12 @@ describe('AsyncExpectation', function() {
|
|||||||
it("prepends the context to the generated failure message", function() {
|
it("prepends the context to the generated failure message", function() {
|
||||||
jasmine.getEnv().requirePromises();
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
|
||||||
.and.returnValue(Promise.resolve({pass: false}));
|
|
||||||
|
|
||||||
var util = {
|
var util = {
|
||||||
buildFailureMessage: function() { return 'failure message'; }
|
buildFailureMessage: function() { return 'failure message'; }
|
||||||
},
|
},
|
||||||
addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
actual = dummyPromise(),
|
|
||||||
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
actual: actual,
|
actual: Promise.reject('rejected'),
|
||||||
addExpectationResult: addExpectationResult,
|
addExpectationResult: addExpectationResult,
|
||||||
util: util
|
util: util
|
||||||
});
|
});
|
||||||
@@ -421,41 +112,35 @@ describe('AsyncExpectation', function() {
|
|||||||
it("prepends the context to a custom failure message", function() {
|
it("prepends the context to a custom failure message", function() {
|
||||||
jasmine.getEnv().requirePromises();
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
|
||||||
.and.returnValue(Promise.resolve({pass: false, message: 'msg'}));
|
|
||||||
|
|
||||||
var util = {
|
var util = {
|
||||||
buildFailureMessage: function() { return 'failure message'; }
|
buildFailureMessage: function() { return 'failure message'; }
|
||||||
},
|
},
|
||||||
addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
actual = dummyPromise(),
|
|
||||||
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
actual: actual,
|
actual: Promise.reject('b'),
|
||||||
addExpectationResult: addExpectationResult,
|
addExpectationResult: addExpectationResult,
|
||||||
util: util
|
util: util
|
||||||
});
|
});
|
||||||
|
|
||||||
return expectation.withContext('Some context').toBeResolved()
|
return expectation.withContext('Some context').toBeResolvedTo('a')
|
||||||
.then(
|
.then(
|
||||||
function() {
|
function() {
|
||||||
expect(addExpectationResult).toHaveBeenCalledWith(false,
|
expect(addExpectationResult).toHaveBeenCalledWith(false,
|
||||||
jasmine.objectContaining({
|
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() {
|
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();
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
|
||||||
.and.returnValue(Promise.resolve({pass: false, message: function() { return 'msg'; } }));
|
|
||||||
|
|
||||||
var util = {
|
var util = {
|
||||||
buildFailureMessage: function() { return 'failure message'; }
|
buildFailureMessage: function() { return 'failure message'; }
|
||||||
},
|
},
|
||||||
addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
actual = dummyPromise(),
|
actual = Promise.reject(),
|
||||||
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
actual: actual,
|
actual: actual,
|
||||||
addExpectationResult: addExpectationResult,
|
addExpectationResult: addExpectationResult,
|
||||||
@@ -475,11 +160,8 @@ describe('AsyncExpectation', function() {
|
|||||||
it("works with #not", function() {
|
it("works with #not", function() {
|
||||||
jasmine.getEnv().requirePromises();
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
|
||||||
.and.returnValue(Promise.resolve({pass: true}));
|
|
||||||
|
|
||||||
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
actual = dummyPromise(),
|
actual = Promise.resolve(),
|
||||||
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
actual: actual,
|
actual: actual,
|
||||||
addExpectationResult: addExpectationResult,
|
addExpectationResult: addExpectationResult,
|
||||||
@@ -499,23 +181,20 @@ describe('AsyncExpectation', function() {
|
|||||||
it("works with #not and a custom message", function() {
|
it("works with #not and a custom message", function() {
|
||||||
jasmine.getEnv().requirePromises();
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
|
||||||
.and.returnValue(Promise.resolve({pass: true, message: 'msg'}));
|
|
||||||
|
|
||||||
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
actual = dummyPromise(),
|
actual = Promise.resolve('a'),
|
||||||
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
actual: actual,
|
actual: actual,
|
||||||
addExpectationResult: addExpectationResult,
|
addExpectationResult: addExpectationResult,
|
||||||
util: jasmineUnderTest.matchersUtil
|
util: jasmineUnderTest.matchersUtil
|
||||||
});
|
});
|
||||||
|
|
||||||
return expectation.withContext('Some context').not.toBeResolved()
|
return expectation.withContext('Some context').not.toBeResolvedTo('a')
|
||||||
.then(
|
.then(
|
||||||
function() {
|
function() {
|
||||||
expect(addExpectationResult).toHaveBeenCalledWith(false,
|
expect(addExpectationResult).toHaveBeenCalledWith(false,
|
||||||
jasmine.objectContaining({
|
jasmine.objectContaining({
|
||||||
message: 'Some context: msg'
|
message: "Some context: Expected a promise not to be resolved to 'a'."
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
23
spec/core/matchers/async/toBeRejectedSpec.js
Normal file
23
spec/core/matchers/async/toBeRejectedSpec.js
Normal file
@@ -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}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
63
spec/core/matchers/async/toBeRejectedWithSpec.js
Normal file
63
spec/core/matchers/async/toBeRejectedWithSpec.js
Normal file
@@ -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}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
23
spec/core/matchers/async/toBeResolvedSpec.js
Normal file
23
spec/core/matchers/async/toBeResolvedSpec.js
Normal file
@@ -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}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
66
spec/core/matchers/async/toBeResolvedToSpec.js
Normal file
66
spec/core/matchers/async/toBeResolvedToSpec.js
Normal file
@@ -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}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -22,15 +22,14 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
if (!j$.isPromiseLike(this.actual)) {
|
if (!j$.isPromiseLike(this.actual)) {
|
||||||
throw new Error('Expected expectAsync to be called with a promise.');
|
throw new Error('Expected expectAsync to be called with a promise.');
|
||||||
}
|
}
|
||||||
|
|
||||||
['toBeResolved', 'toBeRejected', 'toBeResolvedTo', 'toBeRejectedWith'].forEach(wrapCompare.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapCompare(name) {
|
function wrapCompare(name, matcherFactory) {
|
||||||
var matcher = this[name];
|
return function() {
|
||||||
this[name] = function() {
|
|
||||||
var self = this;
|
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);
|
args.unshift(this.actual);
|
||||||
|
|
||||||
// Capture the call stack here, before we go async, so that it will
|
// Capture the call stack here, before we go async, so that it will
|
||||||
@@ -38,155 +37,48 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
// of Jasmine.
|
// of Jasmine.
|
||||||
var errorForStack = j$.util.errorWithStack();
|
var errorForStack = j$.util.errorWithStack();
|
||||||
|
|
||||||
var matcherCompare = this.instantiateMatcher(matcher);
|
var matcherCompare = this.instantiateMatcher(matcherFactory);
|
||||||
|
|
||||||
return matcherCompare.apply(self, args).then(function(result) {
|
return matcherCompare.apply(self, args).then(function(result) {
|
||||||
var message;
|
|
||||||
|
|
||||||
args[0] = promiseForMessage;
|
args[0] = promiseForMessage;
|
||||||
message = j$.Expectation.prototype.buildMessage.call(self, result, name, args);
|
self.processResult(result, name, expected, args, errorForStack);
|
||||||
|
|
||||||
self.addExpectationResult(result.pass, {
|
|
||||||
matcherName: name,
|
|
||||||
passed: result.pass,
|
|
||||||
message: message,
|
|
||||||
error: undefined,
|
|
||||||
errorForStack: errorForStack,
|
|
||||||
actual: self.actual
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
AsyncExpectation.prototype.instantiateMatcher = function(matcher) {
|
AsyncExpectation.prototype.processResult = function(result, name, expected, args, errorForStack) {
|
||||||
var comparisonFunc = this.filters.selectComparisonFunc(matcher);
|
var message = this.buildMessage(result, name, args);
|
||||||
return comparisonFunc || matcher;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
if (expected.length === 1) {
|
||||||
* Expect a promise to be resolved.
|
expected = expected[0];
|
||||||
* @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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return actualPromise.then(
|
this.addExpectationResult(
|
||||||
function(actualValue) {
|
result.pass,
|
||||||
if (self.util.equals(actualValue, expectedValue, self.customEqualityTesters)) {
|
{
|
||||||
return {
|
matcherName: name,
|
||||||
pass: true,
|
passed: result.pass,
|
||||||
message: prefix(true) + '.'
|
message: message,
|
||||||
};
|
error: undefined,
|
||||||
} else {
|
errorForStack: errorForStack,
|
||||||
return {
|
actual: this.actual,
|
||||||
pass: false,
|
expected: expected // TODO: this may need to be arrayified/sliced
|
||||||
message: prefix(false) + ' but it was resolved to ' + j$.pp(actualValue) + '.'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
},
|
|
||||||
function() {
|
|
||||||
return {
|
|
||||||
pass: false,
|
|
||||||
message: prefix(false) + ' but it was rejected.'
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
AsyncExpectation.prototype.buildMessage = j$.Expectation.prototype.buildMessage;
|
||||||
* 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;
|
|
||||||
|
|
||||||
function prefix(passed) {
|
AsyncExpectation.prototype.instantiateMatcher = j$.Expectation.prototype.instantiateMatcher;
|
||||||
return 'Expected a promise ' +
|
|
||||||
(passed ? 'not ' : '') +
|
AsyncExpectation.prototype.addFilter = j$.Expectation.prototype.addFilter;
|
||||||
'to be rejected with ' + j$.pp(expectedValue);
|
|
||||||
|
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) {
|
AsyncExpectation.factory = function(options) {
|
||||||
@@ -205,7 +97,7 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
var negatingFilter = {
|
var negatingFilter = {
|
||||||
selectComparisonFunc: function(matcher) {
|
selectComparisonFunc: function(matcher) {
|
||||||
function defaultNegativeCompare() {
|
function defaultNegativeCompare() {
|
||||||
return matcher.apply(this, arguments).then(function(result) {
|
return matcher.compare.apply(this, arguments).then(function(result) {
|
||||||
result.pass = !result.pass;
|
result.pass = !result.pass;
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -193,6 +193,7 @@ getJasmineRequireObj().Env = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
j$.Expectation.addCoreMatchers(j$.matchers);
|
j$.Expectation.addCoreMatchers(j$.matchers);
|
||||||
|
j$.AsyncExpectation.addCoreMatchers(j$.asyncMatchers);
|
||||||
|
|
||||||
var nextSpecId = 0;
|
var nextSpecId = 0;
|
||||||
var getNextSpecId = function() {
|
var getNextSpecId = function() {
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ getJasmineRequireObj().Expectation = function(j$) {
|
|||||||
expected = expected[0];
|
expected = expected[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: how many of these params are needed?
|
|
||||||
this.addExpectationResult(
|
this.addExpectationResult(
|
||||||
result.pass,
|
result.pass,
|
||||||
{
|
{
|
||||||
|
|||||||
22
src/core/matchers/async/toBeRejected.js
Normal file
22
src/core/matchers/async/toBeRejected.js
Normal file
@@ -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}; }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
46
src/core/matchers/async/toBeRejectedWith.js
Normal file
46
src/core/matchers/async/toBeRejectedWith.js
Normal file
@@ -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) + '.'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
22
src/core/matchers/async/toBeResolved.js
Normal file
22
src/core/matchers/async/toBeResolved.js
Normal file
@@ -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}; }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
46
src/core/matchers/async/toBeResolvedTo.js
Normal file
46
src/core/matchers/async/toBeResolvedTo.js
Normal file
@@ -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.'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
16
src/core/matchers/requireAsyncMatchers.js
Normal file
16
src/core/matchers/requireAsyncMatchers.js
Normal file
@@ -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;
|
||||||
|
};
|
||||||
@@ -72,6 +72,7 @@ var getJasmineRequireObj = (function (jasmineGlobal) {
|
|||||||
j$.NotEmpty = jRequire.NotEmpty(j$);
|
j$.NotEmpty = jRequire.NotEmpty(j$);
|
||||||
|
|
||||||
j$.matchers = jRequire.requireMatchers(jRequire, j$);
|
j$.matchers = jRequire.requireMatchers(jRequire, j$);
|
||||||
|
j$.asyncMatchers = jRequire.requireAsyncMatchers(jRequire, j$);
|
||||||
|
|
||||||
return j$;
|
return j$;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user