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

chore: migrate the Config type test to TSTyche #14687

Merged
merged 9 commits into from
Dec 25, 2023

Conversation

mrazauskas
Copy link
Contributor

@mrazauskas mrazauskas commented Nov 9, 2023

Summary

This PR is migrating the Config type test to TSTyche.


Some two years ago I thought to develop a type testing tool from scratch: jest-community/jest-runner-tsd#32.

Honestly excited to open the very first PR to see TSTyche in action.

Few notable features of TSTyche:

  • possibility to specify TypeScript versions to test on: tstyche --target 5.0,latest;
  • helpers like test() and describe() which support .only, .skip run mode flags;
  • expect style assertions to can compare two types, or two expressions, or an expression with a type;
  • and more.

I decided to publish a beta release first to check how well it works with larger code bases in CI. Otherwise the type testing assertions are stable, well tested, and everything thoroughly documented at https://tstyche.org.

Test plan

Green CI.

Copy link

netlify bot commented Nov 9, 2023

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 14d452b
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/658986b24782230008b83a3b
😎 Deploy Preview https://deploy-preview-14687--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Comment on lines +30 to +33
testMatch: [
'**/__typetests__/**/*.test.ts',
'!**/packages/jest-types/__typetests__/config.test.ts',
],
Copy link
Contributor Author

@mrazauskas mrazauskas Nov 9, 2023

Choose a reason for hiding this comment

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

This looks clumsy for now, but the diff is more readable.

@mrazauskas
Copy link
Contributor Author

mrazauskas commented Nov 9, 2023

CI ran tstyche --target 5.0,latest command and here its output:

Screenshot 2023-11-09 at 16 49 06

Comment on lines +59 to +61
expect(config).type.toBeAssignable({
fakeTimers: {
advanceTimers: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comparing inferred type with and inferred type. Looks simple, but that was not possible with tsd.

@SimenB
Copy link
Member

SimenB commented Nov 15, 2023

Very exciting stuff!

Would it be possible to create a Jest runner for this instead of using the CLI directly? I really like just using jest instead of a separate binary.


The tests themselves look awesome 👍

@mrazauskas
Copy link
Contributor Author

mrazauskas commented Nov 15, 2023

In the beginning this was a runner, but jest --target 5.0,latest is a trouble. Would be possible to keep target in a config file, but in the future we might want to have more versions in CI. Alright, these can be two config files, but how to tell to TSTyche which config to pick? jest --config some/path is taken. Uff.. In a way jest --runnerConfig.tstyche "{"target": ["5.0", "latest"]}" could work, but tstyche --target 5.0,latest is obviously winning (;

Also how to do tstyche --prune, to remove all installed TypeScript versions (in case if something goes wrong)?

And so on. It was much more job to build a stand alone CLI tool, but that solved many problems.

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.

back from the dead 😅 sorry about the slow response.

Happy to move forward with this if you think the tests are more powerful 👍

Can you update to the latest version?

Comment on lines +42 to +56
'Date' as const,
'hrtime' as const,
'nextTick' as const,
'performance' as const,
'queueMicrotask' as const,
'requestAnimationFrame' as const,
'cancelAnimationFrame' as const,
'requestIdleCallback' as const,
'cancelIdleCallback' as const,
'setImmediate' as const,
'clearImmediate' as const,
'setInterval' as const,
'clearInterval' as const,
'setTimeout' as const,
'clearTimeout' as const,
Copy link
Member

Choose a reason for hiding this comment

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

why do we have to as const them all? can we just do that for the entire array instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as const on the whole array results in different type. In that case this would be a readonly tuple of string literal types, but the option expects an array of string literal types. The test is not passing. (To make the test pass, it is enough to have doNotFake: [...doNotFake], but that looks odd.)

I work on one idea which could improve this in the future. For now this is an assignability test and that is how it currently works. I mean, it works and it tests the right thing! That is very good for the first release of TSTyche. And future will bring in improvements for sure.

@mrazauskas
Copy link
Contributor Author

mrazauskas commented Dec 25, 2023

back from the dead 😅 sorry about the slow response.

Happy to move forward with this if you think the tests are more powerful 👍

Can you update to the latest version?

Done. The latest version of TSTyche is pinned. It has only few minor improvements.

By the way, tstyche --target latest fetches the manifest from NPM and picks up the latest TypeScript version from there. In CI it will always use latest, because the current command is tstyche --target 5.0,latest. It can be that at some point tests might not pass on freshly released version of TypeScript.

Recently I added --target current. It runs tests on currently installed TypeScript version (determined via require("typescript").version). In this case type tests could only break if TypeScript is updated in the repo (or a change is introducing a regression).

Not sure which strategy is better. Perhaps CI could run with --target 5.0,current and an additional daily cron job with --target latest,next could be set up? This would mean less random fails in unrelated CIs. Plus makes sure that types work with future releases of TypeScript.

@SimenB SimenB merged commit 35f45f3 into jestjs:main Dec 25, 2023
70 of 73 checks passed
@mrazauskas mrazauskas deleted the migrate-config-type-test-to-tstyche branch December 25, 2023 16:56
Copy link

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 Jan 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants