fix(core): use addCustomEqualityTester instead of overriding toEqual (#22983)

This propagates other custom equality testers added by users. Additionally, if
an Angular project is using jasmine 2.6+, it will allow Jasmine's custom object
differ to print out pretty test error messages.

fixes #22939

PR Close #22983
This commit is contained in:
Kevin Villela 2018-03-25 12:11:49 -07:00 committed by Miško Hevery
parent c94a2c9e3f
commit 0922228024
3 changed files with 37 additions and 22 deletions

View File

@ -21,6 +21,18 @@ class SpyTestObj extends SpyObject {
{ {
describe('testing', () => { describe('testing', () => {
describe('should respect custom equality tester', () => {
beforeEach(() => {
const equalIfMarried =
(first: any, second: any) => { return first === 'kevin' && second === 'patricia'; };
jasmine.addCustomEqualityTester(equalIfMarried);
});
it('for positive test', () => { expect('kevin').toEqual('patricia'); });
it('for negative test', () => { expect('kevin').not.toEqual('kevin'); });
});
describe('equality', () => { describe('equality', () => {
it('should structurally compare objects', () => { it('should structurally compare objects', () => {
const expected = new TestObj(new TestObj({'one': [1, 2]})); const expected = new TestObj(new TestObj({'one': [1, 2]}));

View File

@ -112,29 +112,23 @@ export const expect: (actual: any) => NgMatchers = <any>_global.expect;
}; };
_global.beforeEach(function() { _global.beforeEach(function() {
jasmine.addMatchers({ // Custom handler for Map as we use Jasmine 2.4, and support for maps is not
// Custom handler for Map as Jasmine does not support it yet // added until Jasmine 2.6.
toEqual: function(util) { jasmine.addCustomEqualityTester(function compareMap(actual: any, expected: any): boolean {
return { if (actual instanceof Map) {
compare: function(actual: any, expected: any) { let pass = actual.size === expected.size;
return {pass: util.equals(actual, expected, [compareMap])}; if (pass) {
} actual.forEach((v: any, k: any) => {
}; pass = pass && jasmine.matchersUtil.equals(v, expected.get(k));
});
function compareMap(actual: any, expected: any): boolean {
if (actual instanceof Map) {
let pass = actual.size === expected.size;
if (pass) {
actual.forEach((v: any, k: any) => { pass = pass && util.equals(v, expected.get(k)); });
}
return pass;
} else {
// TODO(misko): we should change the return, but jasmine.d.ts is not null safe
return undefined !;
}
} }
}, return pass;
} else {
// TODO(misko): we should change the return, but jasmine.d.ts is not null safe
return undefined !;
}
});
jasmine.addMatchers({
toBePromise: function() { toBePromise: function() {
return { return {
compare: function(actual: any) { compare: function(actual: any) {

9
packages/types.d.ts vendored
View File

@ -25,3 +25,12 @@ declare namespace jasmine {
toHaveProperties(obj: any): boolean; toHaveProperties(obj: any): boolean;
} }
} }
/**
*Jasmine matching utilities. These are added in the a more recent version of
*the Jasmine typedefs than what we are using:
*https://github.com/DefinitelyTyped/DefinitelyTyped/pull/20771
*/
declare namespace jasmine {
const matchersUtil: MatchersUtil;
}