Skip to content

docs: update expect.extend examples #13195

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

Merged
merged 5 commits into from
Aug 31, 2022
Merged

docs: update expect.extend examples #13195

merged 5 commits into from
Aug 31, 2022

Conversation

mrazauskas
Copy link
Contributor

Summary

Currently the TypeScript examples of expect.extend usage are the only ones in documentation where @types/jest is referenced. Elsewhere types imported from @jest/globals are used. Perhaps it is time to update these last examples as well?

The @types/jest related documentation will stay in old docs. Can be linked quickly if someone opens an issue. Also I am very interested to see DefinitelyTyped/DefinitelyTyped#62037 landing with Jest v30 release. Some work has to be done to make that happen, but in general where will be just one way to extend the Matchers interface. And it this documented in this PR (;

Test plan

Tried out both JS and TS examples locally.

Comment on lines -11 to +12
const toBeWithinRange: MatcherFunction<[floor: number, ceiling: number]> =
function (actual: unknown, floor: unknown, ceiling: unknown) {
const toBeWithinRange: MatcherFunction<[floor: unknown, ceiling: unknown]> =
function (actual, floor, ceiling) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cleaning up. I remember my thinking that expect.extend could grab types from MatcherFunction generics. Not sure if that was good idea. It involves lots of complexity (also in documentation), but has rather limited usage. For instance, does not work if expect is not imported after calling expect.extend. Fine for something small, but does not work with setupFilesAfterEnv. Better to stick with the declaration – single solution which works in many situations.

To enable the matcher for all tests, move the `expect.extend` call to a [`setupFilesAfterEnv`](Configuration.md/#setupfilesafterenv-array) script:

```js
import {expect} from '@jest/globals';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried it out. Jest will transpile setupFilesAfterEnv scripts, so perhaps this can stay in ESM / TS for simplicity.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

I like it! 😀

@SimenB SimenB merged commit 2b1cda9 into jestjs:main Aug 31, 2022
@mrazauskas mrazauskas deleted the docs-update-expect.extend branch August 31, 2022 11:02
@mrazauskas
Copy link
Contributor Author

Happy about that! I had to do this some time ago. Well.. Better later than never ;D

@github-actions
Copy link

github-actions bot commented Oct 1, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants