From 5c9c82cb5186b8453793ff010758fc6fb50a77f2 Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Sat, 26 May 2018 14:28:39 +0200 Subject: [PATCH] Jasmine: Adds negativeCompare to CustomMatcher Jasmine allows a CustomMatcher to define an optional `negativeCompare` method, to handle the `.not` assertions. See https://jasmine.github.io/2.8/custom_matcher.html#section-Custom_negative_comparators --- types/jasmine/index.d.ts | 2 ++ types/jasmine/jasmine-tests.ts | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/types/jasmine/index.d.ts b/types/jasmine/index.d.ts index cd1d5de31e..37e1fd2b14 100644 --- a/types/jasmine/index.d.ts +++ b/types/jasmine/index.d.ts @@ -221,6 +221,8 @@ declare namespace jasmine { interface CustomMatcher { compare(actual: T, expected: T, ...args: any[]): CustomMatcherResult; compare(actual: any, ...expected: any[]): CustomMatcherResult; + negativeCompare?(actual: T, expected: T, ...args: any[]): CustomMatcherResult; + negativeCompare?(actual: any, ...expected: any[]): CustomMatcherResult; } type CustomMatcherFactory = (util: MatchersUtil, customEqualityTesters: CustomEqualityTester[]) => CustomMatcher; diff --git a/types/jasmine/jasmine-tests.ts b/types/jasmine/jasmine-tests.ts index 54e97ed73e..e96293ced4 100644 --- a/types/jasmine/jasmine-tests.ts +++ b/types/jasmine/jasmine-tests.ts @@ -968,7 +968,13 @@ var customMatchers: jasmine.CustomMatcherFactories = { return { compare: (actual: any, floor: number, ceiling: number): jasmine.CustomMatcherResult => { const pass = actual >= floor && actual <= ceiling; - const message = `expected ${actual} ${pass ? 'not ' : ''} to be within range ${floor}-${ceiling}`; + const message = `expected ${actual} to be within range ${floor}-${ceiling}`; + return { message, pass }; + }, + + negativeCompare: (actual: any, floor: number, ceiling: number): jasmine.CustomMatcherResult => { + const pass = actual < floor && actual > ceiling; + const message = `expected ${actual} not to be within range ${floor}-${ceiling}`; return { message, pass }; } }; @@ -1011,6 +1017,14 @@ describe("Custom matcher: 'toBeGoofy'", () => { expect(2).toBeWithinRange(1, 3); }); + it("can use the custom negativeCompare method", () => { + const matcher = customMatchers["toBeWithinRange"](jasmine.matchersUtil, []); + const result = matcher.negativeCompare!(1, 2, 3); + + expect(result.pass).toBe(false); + expect(result.message).toBe("expected 1 not to be within range 2-3"); + }); + it("can be negated", () => { expect({ hyuk: 'this is fun'