From 3bdc4ff651d4d1549f58d1893a18fc22c96839e1 Mon Sep 17 00:00:00 2001 From: Jamie Mason Date: Sun, 17 Dec 2017 19:47:38 +0000 Subject: [PATCH] Jasmine matchers can accept many arguments refs #21327 --- types/jasmine/index.d.ts | 4 ++-- types/jasmine/jasmine-tests.ts | 14 ++++++++++++++ types/jest/index.d.ts | 4 ++-- types/jest/v16/index.d.ts | 4 ++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/types/jasmine/index.d.ts b/types/jasmine/index.d.ts index c1c15e45de..cc28931542 100644 --- a/types/jasmine/index.d.ts +++ b/types/jasmine/index.d.ts @@ -219,8 +219,8 @@ declare namespace jasmine { type CustomEqualityTester = (first: any, second: any) => boolean | void; interface CustomMatcher { - compare(actual: T, expected: T): CustomMatcherResult; - compare(actual: any, expected: any): CustomMatcherResult; + compare(actual: T, expected: T, ...args: any[]): CustomMatcherResult; + compare(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 ab132d3cee..b54269aa61 100644 --- a/types/jasmine/jasmine-tests.ts +++ b/types/jasmine/jasmine-tests.ts @@ -963,6 +963,15 @@ var customMatchers: jasmine.CustomMatcherFactories = { return result; } }; + }, + toBeWithinRange: (util: jasmine.MatchersUtil, customEqualityTesters: jasmine.CustomEqualityTester[]) => { + 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}`; + return { message, pass }; + } + }; } }; // add the custom matchers to interface jasmine.Matchers via TypeScript declaration merging @@ -977,6 +986,7 @@ var customMatchers: jasmine.CustomMatcherFactories = { declare namespace jasmine { interface Matchers { toBeGoofy(expected?: jasmine.Expected): boolean; + toBeWithinRange(expected?: jasmine.Expected, floor?: number, ceiling?: number): boolean; } } @@ -997,6 +1007,10 @@ describe("Custom matcher: 'toBeGoofy'", () => { }).toBeGoofy({ hyuk: ' is fun' }); }); + it("can take many 'expected' parameters", () => { + expect(2).toBeWithinRange(1, 3); + }); + it("can be negated", () => { expect({ hyuk: 'this is fun' diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 5c2152694f..0befb10e33 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -714,8 +714,8 @@ declare namespace jasmine { type CustomEqualityTester = (first: any, second: any) => boolean; interface CustomMatcher { - compare(actual: T, expected: T): CustomMatcherResult; - compare(actual: any, expected: any): CustomMatcherResult; + compare(actual: T, expected: T, ...args: any[]): CustomMatcherResult; + compare(actual: any, ...expected: any[]): CustomMatcherResult; } interface CustomMatcherResult { diff --git a/types/jest/v16/index.d.ts b/types/jest/v16/index.d.ts index f376ea5bcc..c1282c2c74 100644 --- a/types/jest/v16/index.d.ts +++ b/types/jest/v16/index.d.ts @@ -287,8 +287,8 @@ declare namespace jasmine { } interface CustomMatcher { - compare(actual: T, expected: T): CustomMatcherResult; - compare(actual: any, expected: any): CustomMatcherResult; + compare(actual: T, expected: T, ...args: any[]): CustomMatcherResult; + compare(actual: any, ...expected: any[]): CustomMatcherResult; } interface CustomMatcherResult {