Skip to content
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

feat(jest-expect): re-export Matchers and MatcherFunctionWithState interface #12418

Merged
merged 5 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions examples/expect-extend/toBeWithinRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {expect} from '@jest/globals';
import type {MatcherFunction} from 'expect';
import {type MatcherFunction, expect} from 'expect';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this? If 'expect' is extended later, perhaps importing from 'expect' here makes sense? Or?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least, I agree if this example is "extend expect" and not "extend jest with extra matcher". In that case people should import from @jest/globals (or use the global whenever we fix that)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. Perhaps I will revert this. In my head this was "extend jest with extra matcher" example. I mean, there is an extra file where this new matcher is used in a test. If expect was extended not for Jest, then expect(100).toBeWithinRange(90, 110); line in this file would be enough without extra test file. It makes sense to import from '@jest/globals'.

Copy link
Member

@SimenB SimenB Feb 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

We should work on https://jestjs.io/docs/expect to give better examples there probably, but keep this example as is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed (;


const toBeWithinRange: MatcherFunction<[floor: number, ceiling: number]> =
function (actual: unknown, floor: unknown, ceiling: unknown) {
Expand Down Expand Up @@ -48,9 +47,9 @@ expect.extend({

declare module 'expect' {
interface AsymmetricMatchers {
toBeWithinRange(a: number, b: number): void;
toBeWithinRange(floor: number, ceiling: number): void;
}
interface Matchers<R> {
toBeWithinRange(a: number, b: number): R;
toBeWithinRange(floor: number, ceiling: number): R;
}
}
6 changes: 6 additions & 0 deletions examples/expect-extend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"strict": true
},
"include": ["./**/*"]
}
8 changes: 7 additions & 1 deletion packages/jest-expect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import {
} from 'jest-snapshot';
import type {JestExpect} from './types';

export type {AsymmetricMatchers, MatcherFunction, MatcherState} from 'expect';
export type {
AsymmetricMatchers,
Matchers,
MatcherFunction,
MatcherFunctionWithState,
MatcherState,
} from 'expect';
export type {JestExpect} from './types';

function createJestExpect(): JestExpect {
Expand Down