Provide better diffs for object graphs that include objectContaining

Turns this output:
    Expected $[0].foo = Object({ a: 4, b: 5 }) to equal <jasmine.objectContaining(Object({ a: 1, c: 3 }))>.

into this:
    Expected $[0].foo.a = 4 to equal 1.
    Expected $[0].foo.c = undefined to equal 3.

And turns this output:
    Expected spy jasmineDone to have been called with:
       [ ... snipped very long expected call ]
    but actual calls were:
       [ ... snipped very long actual call ]

    Call 0:
      Expected $[0] = Object({ overallStatus: 'failed', totalTime: 1, incompleteReason: undefined, order: Order({ random: true, seed: '88732', sort: Function }), failedExpectations: [ Object({ matcherName: 'toBeResolved', passed: false, message: 'Suite "a suite" ran a "toBeResolved" expectation after it finished.
      Did you forget to return or await the result of expectAsync?', error: undefined, errorForStack: Error, actual: [object Promise], expected: [  ], globalErrorType: 'lateExpeztation' }) ], deprecationWarnings: [  ] }) to equal <jasmine.objectContaining(Object({ failedExpectations: [ <jasmine.objectContaining(Object({ passed: false, globalErrorType: 'lateExpectation', message: 'Suite "a suite" ran a "toBeResolved" expectation after it finished.
      Did you forget to return or await the result of expectAsync?', matcherName: 'toBeResolved' }))> ] }))>.

into this:
    Expected spy jasmineDone to have been called with:
       [ ... snipped very long expected call ]
    but actual calls were:
       [ ... snipped very long actual call ]

    Call 0:
      Expected $[0].failedExpectations[0].globalErrorType = 'lateExpeztation' to equal 'lateExpectation'.
This commit is contained in:
Steve Gravrock
2019-09-28 17:50:17 -07:00
parent 10dbf8be98
commit 472f61ab37
6 changed files with 181 additions and 6 deletions

View File

@@ -41,6 +41,27 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
return true;
};
ObjectContaining.prototype.valuesForDiff_ = function(other) {
if (!j$.isObject_(other)) {
return {
self: this.jasmineToString(),
other: other
};
}
var filteredOther = {};
Object.keys(this.sample).forEach(function (k) {
// eq short-circuits comparison of objects that have different key sets,
// so include all keys even if undefined.
filteredOther[k] = other[k];
});
return {
self: this.sample,
other: filteredOther
};
};
ObjectContaining.prototype.jasmineToString = function() {
return '<jasmine.objectContaining(' + j$.pp(this.sample) + ')>';
};

View File

@@ -55,7 +55,16 @@ getJasmineRequireObj().matchersUtil = function(j$) {
return obj && j$.isA_('Function', obj.asymmetricMatch);
}
function asymmetricMatch(a, b, customTesters, diffBuilder) {
function asymmetricDiff(a, b, aStack, bStack, customTesters, diffBuilder) {
if (j$.isFunction_(b.valuesForDiff_)) {
var values = b.valuesForDiff_(a);
eq(values.other, values.self, aStack, bStack, customTesters, diffBuilder);
} else {
diffBuilder.record(a, b);
}
}
function asymmetricMatch(a, b, aStack, bStack, customTesters, diffBuilder) {
var asymmetricA = isAsymmetric(a),
asymmetricB = isAsymmetric(b),
result;
@@ -67,6 +76,8 @@ getJasmineRequireObj().matchersUtil = function(j$) {
if (asymmetricA) {
result = a.asymmetricMatch(b, customTesters);
if (!result) {
// TODO: Do we want to build an asymmetric diff when the actual was an
// asymmeteric equality tester? Might be confusing.
diffBuilder.record(a, b);
}
return result;
@@ -75,7 +86,7 @@ getJasmineRequireObj().matchersUtil = function(j$) {
if (asymmetricB) {
result = b.asymmetricMatch(a, customTesters);
if (!result) {
diffBuilder.record(a, b);
asymmetricDiff(a, b, aStack, bStack, customTesters, diffBuilder);
}
return result;
}
@@ -93,7 +104,7 @@ getJasmineRequireObj().matchersUtil = function(j$) {
function eq(a, b, aStack, bStack, customTesters, diffBuilder) {
var result = true, i;
var asymmetricResult = asymmetricMatch(a, b, customTesters, diffBuilder);
var asymmetricResult = asymmetricMatch(a, b, aStack, bStack, customTesters, diffBuilder);
if (!j$.util.isUndefined(asymmetricResult)) {
return asymmetricResult;
}