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'