-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(expect): expose AsymmetricMatchers
and RawMatcherFn
interfaces
#12363
Changes from all commits
992acd9
7b7fcd6
146f4da
d46987c
3082734
7291941
3a6222e
bc69190
4290c2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {expect, test} from '@jest/globals'; | ||
import '../toBeWithinRange'; | ||
|
||
test('is within range', () => expect(100).toBeWithinRange(90, 110)); | ||
|
||
test('is NOT within range', () => expect(101).not.toBeWithinRange(0, 100)); | ||
|
||
test('asymmetric ranges', () => { | ||
expect({apples: 6, bananas: 3}).toEqual({ | ||
apples: expect.toBeWithinRange(1, 10), | ||
bananas: expect.not.toBeWithinRange(11, 20), | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"private": true, | ||
"version": "0.0.0", | ||
"name": "example-expect-extend", | ||
"devDependencies": { | ||
"@babel/core": "*", | ||
"@babel/preset-env": "*", | ||
"@babel/preset-typescript": "*", | ||
"@jest/globals": "workspace:*", | ||
"babel-jest": "workspace:*", | ||
"expect": "workspace:*", | ||
"jest": "workspace:*" | ||
}, | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"babel": { | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"node": "current" | ||
} | ||
} | ||
], | ||
"@babel/preset-typescript" | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {expect} from '@jest/globals'; | ||
import type {RawMatcherFn} from 'expect'; | ||
|
||
const toBeWithinRange: RawMatcherFn = ( | ||
actual: number, | ||
floor: number, | ||
ceiling: number, | ||
) => { | ||
const pass = actual >= floor && actual <= ceiling; | ||
if (pass) { | ||
return { | ||
message: () => | ||
`expected ${actual} not to be within range ${floor} - ${ceiling}`, | ||
pass: true, | ||
}; | ||
} else { | ||
return { | ||
message: () => | ||
`expected ${actual} to be within range ${floor} - ${ceiling}`, | ||
pass: false, | ||
}; | ||
} | ||
}; | ||
|
||
expect.extend({ | ||
toBeWithinRange, | ||
}); | ||
|
||
declare module 'expect' { | ||
interface AsymmetricMatchers { | ||
toBeWithinRange(a: number, b: number): void; | ||
} | ||
interface Matchers<R> { | ||
toBeWithinRange(a: number, b: number): R; | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,77 @@ | |
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {expectError} from 'tsd-lite'; | ||
import type * as expect from 'expect'; | ||
import {expectError, expectType} from 'tsd-lite'; | ||
import type {EqualsFunction, Tester} from '@jest/expect-utils'; | ||
import {type Matchers, expect} from 'expect'; | ||
import type * as jestMatcherUtils from 'jest-matcher-utils'; | ||
|
||
type M = expect.Matchers<void, unknown>; | ||
type N = expect.Matchers<void>; | ||
type M = Matchers<void, unknown>; | ||
type N = Matchers<void>; | ||
|
||
expectError(() => { | ||
type E = expect.Matchers; | ||
type E = Matchers; | ||
}); | ||
|
||
// extend | ||
|
||
type MatcherUtils = typeof jestMatcherUtils & { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should export this type probably There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm.. But this is just a helper for testing. Perhaps that’s fine? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. I though you speak about exporting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was just some duplication that felt unnecessary, I don't feel strongly here 🙂 |
||
iterableEquality: Tester; | ||
subsetEquality: Tester; | ||
}; | ||
|
||
expectType<void>( | ||
expect.extend({ | ||
toBeWithinRange(actual: number, floor: number, ceiling: number) { | ||
expectType<number>(this.assertionCalls); | ||
expectType<string | undefined>(this.currentTestName); | ||
expectType<(() => void) | undefined>(this.dontThrow); | ||
expectType<Error | undefined>(this.error); | ||
expectType<EqualsFunction>(this.equals); | ||
expectType<boolean | undefined>(this.expand); | ||
expectType<number | null | undefined>(this.expectedAssertionsNumber); | ||
expectType<Error | undefined>(this.expectedAssertionsNumberError); | ||
expectType<boolean | undefined>(this.isExpectingAssertions); | ||
expectType<Error | undefined>(this.isExpectingAssertionsError); | ||
expectType<boolean>(this.isNot); | ||
expectType<string>(this.promise); | ||
expectType<Array<Error>>(this.suppressedErrors); | ||
expectType<string | undefined>(this.testPath); | ||
expectType<MatcherUtils>(this.utils); | ||
|
||
const pass = actual >= floor && actual <= ceiling; | ||
if (pass) { | ||
return { | ||
message: () => | ||
`expected ${actual} not to be within range ${floor} - ${ceiling}`, | ||
pass: true, | ||
}; | ||
} else { | ||
return { | ||
message: () => | ||
`expected ${actual} to be within range ${floor} - ${ceiling}`, | ||
pass: false, | ||
}; | ||
} | ||
}, | ||
}), | ||
); | ||
|
||
declare module 'expect' { | ||
interface AsymmetricMatchers { | ||
toBeWithinRange(floor: number, ceiling: number): void; | ||
mrazauskas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
interface Matchers<R> { | ||
toBeWithinRange(floor: number, ceiling: number): void; | ||
} | ||
} | ||
|
||
expectType<void>(expect(100).toBeWithinRange(90, 110)); | ||
expectType<void>(expect(101).not.toBeWithinRange(0, 100)); | ||
|
||
expectType<void>( | ||
expect({apples: 6, bananas: 3}).toEqual({ | ||
apples: expect.toBeWithinRange(1, 10), | ||
bananas: expect.not.toBeWithinRange(11, 20), | ||
}), | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,16 @@ import {INTERNAL_MATCHER_FLAG} from './jestMatchersObject'; | |
|
||
export type SyncExpectationResult = { | ||
pass: boolean; | ||
message: () => string; | ||
message(): string; | ||
}; | ||
|
||
export type AsyncExpectationResult = Promise<SyncExpectationResult>; | ||
|
||
export type ExpectationResult = SyncExpectationResult | AsyncExpectationResult; | ||
|
||
export type RawMatcherFn<T extends MatcherState = MatcherState> = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need |
||
(this: T, received: any, expected: any, options?: any): ExpectationResult; | ||
(this: T, actual: any, expected: any, options?: any): ExpectationResult; | ||
/** @internal */ | ||
[INTERNAL_MATCHER_FLAG]?: boolean; | ||
}; | ||
|
||
|
@@ -31,7 +32,7 @@ export type PromiseMatcherFn = (actual: any) => Promise<void>; | |
export type MatcherState = { | ||
assertionCalls: number; | ||
currentTestName?: string; | ||
dontThrow?: () => void; | ||
dontThrow?(): void; | ||
error?: Error; | ||
equals: EqualsFunction; | ||
expand?: boolean; | ||
|
@@ -56,7 +57,7 @@ export interface AsymmetricMatcher { | |
toAsymmetricMatcher?(): string; | ||
} | ||
export type MatchersObject<T extends MatcherState = MatcherState> = { | ||
[id: string]: RawMatcherFn<T>; | ||
[name: string]: RawMatcherFn<T>; | ||
}; | ||
export type ExpectedAssertionsErrors = Array<{ | ||
actual: string | number; | ||
|
@@ -73,7 +74,7 @@ export type Expect<State extends MatcherState = MatcherState> = { | |
assertions(numberOfAssertions: number): void; | ||
// TODO: remove this `T extends` - should get from some interface merging | ||
extend<T extends MatcherState = State>(matchers: MatchersObject<T>): void; | ||
extractExpectedAssertionsErrors: () => ExpectedAssertionsErrors; | ||
extractExpectedAssertionsErrors(): ExpectedAssertionsErrors; | ||
getState(): State; | ||
hasAssertions(): void; | ||
setState(state: Partial<State>): void; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no way we can avoid the duplication here, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrestled with this. No luck. Alternative idea: what if
expect.extend()
would returnExpect
extended with types of custom matchers? Right nowexpect.extend()
returnsvoid
. The proposed return value would be only useful for typings. No need to extend, augment, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drafty, but working prototype (in this case,
toBeWithinRange
takes two arguments and both are inferred):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! 😃 We need "old" way to continue working of course, but doing it this way seems smoother