diff --git a/packages/core/test/testing_internal_spec.ts b/packages/core/test/testing_internal_spec.ts index 03320aa72f..9de173b199 100644 --- a/packages/core/test/testing_internal_spec.ts +++ b/packages/core/test/testing_internal_spec.ts @@ -21,6 +21,18 @@ class SpyTestObj extends SpyObject { { 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', () => { it('should structurally compare objects', () => { const expected = new TestObj(new TestObj({'one': [1, 2]})); diff --git a/packages/platform-browser/testing/src/matchers.ts b/packages/platform-browser/testing/src/matchers.ts index 01417f570b..45c51c53e4 100644 --- a/packages/platform-browser/testing/src/matchers.ts +++ b/packages/platform-browser/testing/src/matchers.ts @@ -112,29 +112,23 @@ export const expect: (actual: any) => NgMatchers = _global.expect; }; _global.beforeEach(function() { - jasmine.addMatchers({ - // Custom handler for Map as Jasmine does not support it yet - toEqual: function(util) { - return { - compare: function(actual: any, expected: any) { - return {pass: util.equals(actual, expected, [compareMap])}; - } - }; - - 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 !; - } + // Custom handler for Map as we use Jasmine 2.4, and support for maps is not + // added until Jasmine 2.6. + jasmine.addCustomEqualityTester(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 && jasmine.matchersUtil.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 !; + } + }); + jasmine.addMatchers({ toBePromise: function() { return { compare: function(actual: any) { diff --git a/packages/types.d.ts b/packages/types.d.ts index 1ec1d9f1e9..96996ea8e0 100644 --- a/packages/types.d.ts +++ b/packages/types.d.ts @@ -18,4 +18,19 @@ /// declare let isNode: boolean; -declare let isBrowser: boolean; \ No newline at end of file +declare let isBrowser: boolean; + +declare namespace jasmine { + interface Matchers { + 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; +}