Add custom async matchers
This commit is contained in:
committed by
Steve Gravrock
parent
008b80adc5
commit
f77ee32c56
@@ -266,6 +266,19 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
}
|
||||
};
|
||||
|
||||
this.addAsyncMatchers = function(matchersToAdd) {
|
||||
if (!currentRunnable()) {
|
||||
throw new Error(
|
||||
'Async Matchers must be added in a before function or a spec'
|
||||
);
|
||||
}
|
||||
var customAsyncMatchers =
|
||||
runnableResources[currentRunnable().id].customAsyncMatchers;
|
||||
for (var matcherName in matchersToAdd) {
|
||||
customAsyncMatchers[matcherName] = matchersToAdd[matcherName];
|
||||
}
|
||||
};
|
||||
|
||||
j$.Expectation.addCoreMatchers(j$.matchers);
|
||||
j$.Expectation.addAsyncCoreMatchers(j$.asyncMatchers);
|
||||
|
||||
@@ -297,6 +310,7 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
return j$.Expectation.asyncFactory({
|
||||
util: j$.matchersUtil,
|
||||
customEqualityTesters: runnableResources[spec.id].customEqualityTesters,
|
||||
customAsyncMatchers: runnableResources[spec.id].customAsyncMatchers,
|
||||
actual: actual,
|
||||
addExpectationResult: addExpectationResult
|
||||
});
|
||||
@@ -311,6 +325,7 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
spies: [],
|
||||
customEqualityTesters: [],
|
||||
customMatchers: {},
|
||||
customAsyncMatchers: {},
|
||||
customSpyStrategies: {},
|
||||
defaultStrategyFn: undefined
|
||||
};
|
||||
@@ -322,6 +337,9 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
resources.customMatchers = j$.util.clone(
|
||||
runnableResources[parentRunnableId].customMatchers
|
||||
);
|
||||
resources.customAsyncMatchers = j$.util.clone(
|
||||
runnableResources[parentRunnableId].customAsyncMatchers
|
||||
);
|
||||
resources.defaultStrategyFn =
|
||||
runnableResources[parentRunnableId].defaultStrategyFn;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
getJasmineRequireObj().Expectation = function(j$) {
|
||||
var promiseForMessage = {
|
||||
jasmineToString: function() {
|
||||
return 'a promise';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Matchers that come with Jasmine out of the box.
|
||||
* @namespace matchers
|
||||
@@ -62,8 +56,12 @@ getJasmineRequireObj().Expectation = function(j$) {
|
||||
);
|
||||
}
|
||||
|
||||
if (!j$.isPromiseLike(this.expector.actual)) {
|
||||
throw new Error('Expected expectAsync to be called with a promise.');
|
||||
var customAsyncMatchers = options.customAsyncMatchers || {};
|
||||
for (var matcherName in customAsyncMatchers) {
|
||||
this[matcherName] = wrapAsyncCompare(
|
||||
matcherName,
|
||||
customAsyncMatchers[matcherName]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +111,7 @@ getJasmineRequireObj().Expectation = function(j$) {
|
||||
return this.expector
|
||||
.compare(name, matcherFactory, arguments)
|
||||
.then(function(result) {
|
||||
self.expector.processResult(result, errorForStack, promiseForMessage);
|
||||
self.expector.processResult(result, errorForStack);
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -168,7 +166,7 @@ getJasmineRequireObj().Expectation = function(j$) {
|
||||
return matcher.compare.apply(this, arguments).then(negate);
|
||||
}
|
||||
|
||||
return defaultNegativeCompare;
|
||||
return matcher.negativeCompare || defaultNegativeCompare;
|
||||
},
|
||||
buildFailureMessage: negatedFailureMessage
|
||||
};
|
||||
|
||||
@@ -68,12 +68,7 @@ getJasmineRequireObj().Expector = function(j$) {
|
||||
return result;
|
||||
};
|
||||
|
||||
Expector.prototype.processResult = function(
|
||||
result,
|
||||
errorForStack,
|
||||
actualOverride
|
||||
) {
|
||||
this.args[0] = actualOverride || this.args[0];
|
||||
Expector.prototype.processResult = function(result, errorForStack) {
|
||||
var message = this.buildMessage(result);
|
||||
|
||||
if (this.expected.length === 1) {
|
||||
|
||||
@@ -10,9 +10,12 @@ getJasmineRequireObj().toBeRejected = function(j$) {
|
||||
* @example
|
||||
* return expectAsync(aPromise).toBeRejected();
|
||||
*/
|
||||
return function toBeResolved(util) {
|
||||
return function toBeRejected(util) {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
if (!j$.isPromiseLike(actual)) {
|
||||
throw new Error('Expected toBeRejected to be called on a promise.');
|
||||
}
|
||||
return actual.then(
|
||||
function() { return {pass: false}; },
|
||||
function() { return {pass: true}; }
|
||||
|
||||
@@ -14,6 +14,10 @@ getJasmineRequireObj().toBeRejectedWith = function(j$) {
|
||||
return function toBeRejectedWith(util, customEqualityTesters) {
|
||||
return {
|
||||
compare: function(actualPromise, expectedValue) {
|
||||
if (!j$.isPromiseLike(actualPromise)) {
|
||||
throw new Error('Expected toBeRejectedWith to be called on a promise.');
|
||||
}
|
||||
|
||||
function prefix(passed) {
|
||||
return 'Expected a promise ' +
|
||||
(passed ? 'not ' : '') +
|
||||
|
||||
@@ -17,6 +17,10 @@ getJasmineRequireObj().toBeRejectedWithError = function(j$) {
|
||||
return function toBeRejectedWithError() {
|
||||
return {
|
||||
compare: function(actualPromise, arg1, arg2) {
|
||||
if (!j$.isPromiseLike(actualPromise)) {
|
||||
throw new Error('Expected toBeRejectedWithError to be called on a promise.');
|
||||
}
|
||||
|
||||
var expected = getExpectedFromArgs(arg1, arg2);
|
||||
|
||||
return actualPromise.then(
|
||||
|
||||
@@ -13,6 +13,10 @@ getJasmineRequireObj().toBeResolved = function(j$) {
|
||||
return function toBeResolved(util) {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
if (!j$.isPromiseLike(actual)) {
|
||||
throw new Error('Expected toBeResolved to be called on a promise.');
|
||||
}
|
||||
|
||||
return actual.then(
|
||||
function() { return {pass: true}; },
|
||||
function() { return {pass: false}; }
|
||||
|
||||
@@ -14,6 +14,10 @@ getJasmineRequireObj().toBeResolvedTo = function(j$) {
|
||||
return function toBeResolvedTo(util, customEqualityTesters) {
|
||||
return {
|
||||
compare: function(actualPromise, expectedValue) {
|
||||
if (!j$.isPromiseLike(actualPromise)) {
|
||||
throw new Error('Expected toBeResolvedTo to be called on a promise.');
|
||||
}
|
||||
|
||||
function prefix(passed) {
|
||||
return 'Expected a promise ' +
|
||||
(passed ? 'not ' : '') +
|
||||
|
||||
@@ -302,6 +302,20 @@ getJasmineRequireObj().interface = function(jasmine, env) {
|
||||
return env.addMatchers(matchers);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add custom async matchers for the current scope of specs.
|
||||
*
|
||||
* _Note:_ This is only callable from within a {@link beforeEach}, {@link it}, or {@link beforeAll}.
|
||||
* @name jasmine.addMatchers
|
||||
* @since 3.5.0
|
||||
* @function
|
||||
* @param {Object} matchers - Keys from this object will be the new async matcher names.
|
||||
* @see custom_matcher
|
||||
*/
|
||||
jasmine.addAsyncMatchers = function(matchers) {
|
||||
return env.addAsyncMatchers(matchers);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the currently booted mock {Clock} for this Jasmine environment.
|
||||
* @name jasmine.clock
|
||||
|
||||
Reference in New Issue
Block a user