Merge pull request #26051 from cexbrayat/fix/jasmine-negative-compare

Jasmine: Adds negativeCompare to CustomMatcher
This commit is contained in:
Benjamin Lichtman
2018-05-31 15:34:45 -07:00
committed by GitHub
2 changed files with 17 additions and 1 deletions
+2
View File
@@ -221,6 +221,8 @@ declare namespace jasmine {
interface CustomMatcher {
compare<T>(actual: T, expected: T, ...args: any[]): CustomMatcherResult;
compare(actual: any, ...expected: any[]): CustomMatcherResult;
negativeCompare?<T>(actual: T, expected: T, ...args: any[]): CustomMatcherResult;
negativeCompare?(actual: any, ...expected: any[]): CustomMatcherResult;
}
type CustomMatcherFactory = (util: MatchersUtil, customEqualityTesters: CustomEqualityTester[]) => CustomMatcher;
+15 -1
View File
@@ -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'