-
-
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 5 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": "*", | ||
"babel-jest": "*", | ||
"expect": "*", | ||
"jest": "*" | ||
}, | ||
"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; | ||
} | ||
} | ||
Comment on lines
+36
to
+43
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's no way we can avoid the duplication here, right? 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. Wrestled with this. No luck. Alternative idea: what if import {expect as jestExpect} from '@jest/globals';
import customMatcher from 'customMatcher';
const expect = jestExpect.extend({
customMatcher,
});
// here `expect.customMatcher()` is fully typed 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. I like it! 😃 We need "old" way to continue working of course, but doing it this way seems smoother |
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ 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; | ||
/** @internal */ | ||
[INTERNAL_MATCHER_FLAG]?: boolean; | ||
}; | ||
|
||
|
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.
do we need
workspace:*
here so we resolve to the versions we want?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.
potentially just
resolutions
in root: https://github.com/facebook/jest/blob/60eb4160b5258eed5faf381e0c8ea6662076cc6c/package.json#L157-L159