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/types): infer argument types passed to test and describe functions from each tables #12885

Merged
merged 2 commits into from May 31, 2022

Conversation

mrazauskas
Copy link
Contributor

Summary

This PR is filling one more gap of types. Currently types of arguments passed from each tables to test and describe functions are any. In this field, @types/jest does better job. I was playing with for some time. The solutions is different than the one from DT – somewhat better, but with some limitations. Documentation noting the caveats is included (by the way, types from DT has similar issues).

Probably I will have to revisit it later, but that is a strong start already.

Test plan

The detailed type tests for each now live in a separate file. The type tests for global API were cleaned up and slightly extended. Could not leave them. Apologies for many tests. Just wanted to be sure that everything works.

timeout?: number,
) => void;

<T extends readonly [unknown, ...Array<unknown>]>(table: ReadonlyArray<T>): (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

An array of arrays is actually treated as array of tuples. Spreading ['abc', 123] as tuple gives a type a: string, b: number, spreading the same thing as an array results in a type a: string | number, b: string | number. The hard part was to capture indefinite number of tuple members. [unknown, ...Array<unknown>] looks clumsy, but does this job well.

timeout?: number,
) => void;

<T extends ReadonlyArray<unknown>>(table: ReadonlyArray<T>): (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If a table is defined a variable (not inline), TypeScript treats it as Array<Array<...>>. This overload will catch this case.

timeout?: number,
) => void;

<T extends Record<string, unknown>>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Finally this is here to allow typing template string literals through generic argument.

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.

woah, this is awesome! 👏

@SimenB SimenB merged commit 6277505 into jestjs:main May 31, 2022
@mrazauskas mrazauskas deleted the feat-each-arg-types branch May 31, 2022 06:31
@github-actions
Copy link

github-actions bot commented Jul 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 Jul 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.

None yet

3 participants