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: improve expectTypeOf error messages #4206
Conversation
✅ Deploy Preview for fastidious-cascaron-4ded94 ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
You can also add a link in the docs!
Yeah, it doesn't close the issue yet as there are other improvements that we need to make for typechecking. Overall, PR looks good to me 👍🏻 |
Oh I should update these docs too: https://vitest.dev/guide/testing-types.html#reading-errors |
@sheremet-va ok, added more docs re the error messages: https://github.com/vitest-dev/vitest/pull/4206/files#diff-58c52997981aa78c64ba261e445d5f5866f73205518e556a06beaa2d11fbcbff I also updated typechecker.ts to put the type error on a single line so the useful information is as prominent as possible: https://github.com/vitest-dev/vitest/pull/4206/files#diff-02bca40f9c80e61170f4d69ec1aa9f68ff87c5ba3f0e49f7b5ab760362c9e69d Here's the snapshot result: https://github.com/vitest-dev/vitest/pull/4206/files#diff-86e5c8d303f3cc24cb69cd88757cd83b4076c02f5d4c6b36d2f42792a1808d99 I don't know why the builds are red, but I don't think it's related to this pull request. |
Fixes #17 Closes #36 (by documenting that `toMatchTypeOf` ~= `toExtend`) Closes #37 (by documenting that helper types are not part of the API surface) Closes #32 Closes #4 Closes #6 (by documenting that you _can't_ use `.not.toBeCallableWith(...)`) Closes #38 Use generics to give better error messages than "Arguments for the rest parameter 'MISMATCH' were not provided" for most `toEqualTypeOf` cases and many `toMatchTypeOf` cases. This trades off some implementation complexity for better end-user error messages. Essentially, write a special `<Expected extends ...>` constraint for each overload of each method, which does some crazy conditional logic, which boil down to: - `<Expected extends Actual>` for `toEqualTypeOf` (when we aren't in a `.not` context) - `<Expected extends Satisfies<Actual>>` for `toMatchTypeOf` Anyone interested, have a look at the snapshot updates in `errors.test.ts.snap` to see what the real world difference is. Each of these constraints apply only when we know it's going to "fail" - i.e. `Satisfies<...>` is a fairly meaningless helper type that is used to try to show errors at the type-constraint level rather than the `...MISMATCH: MismatchArgs<...>` level which won't give good error messages. When using `.not`, the constraint just becomes `extends unknown`, and you'll have to squint as before. See also: #14 for the better long-term solution, _if_ the TypeScript team decide to merge the throw types PR. See also: #13 for a hopefully-complementary improvement to the information on hover, which will improve the cases this doesn't cover. TODO: - [x] See if the `expectTypeOf({a: 1}).toMatchTypeOf({a: 'one'})` case can also be improved. - [x] Document. The constraints are a bit different to what most users would be used to, so it's worth highlighting the best way to read error messages and clarify when they might default to "Arguments for the rest parameter 'MISMATCH' were not provided" Note: I have publish v0.17.0-1 based on this PR and will hopefully be able to use [that version in vitest](vitest-dev/vitest#4206) as a test before merging.
@sheremet-va loved the talk(!). I could do with some help with the pnpm lockfile when you get a minute. I ran |
979ac96
to
9f82401
Compare
I think you have the older version installed. I run the command for you, should be fine now. |
Do you think we can have this in Vitest 1.0? |
Are you saying you'd rather hold off on this change until vitest v1.0.0? If so, let me know if there's a alternate branch I should be comparing to, or what I should do. |
I asked when do you think this can be merged? |
Whenever it's green. I'll take another look today or tomorrow to see why it started failing. What's the timeline for v1? |
Thank you.
This month. It can always be merged later, I just wanted to know when you think this is ready since it's still in draft. |
@sheremet-va now passing on all but windows. Looking at the job I don't see what the failure actually is, the test I affected seemed to have passed, but I just pushed a change to account for potential Ready for review at this point, would be great to get any help getting it over the line. |
CI is in a poor state currently, so if your tests are working fine you shouldn't worry. From what I can see here, all the issues are also present on |
@sheremet-va in that case, feel free to merge. If any issues arise I will be able to help, but I'd like to get this in ASAP to avoid future merge conflicts. |
Description
Update expect-type version to the prerelease from mmkal/expect-type#16 - it should make error messages much better. This is hard to see in a repo where the assertions are passing, but see the snapshot update in that PR. (Note: the title is "improve expectTypeOf error messages" rather than referencing the update, since that's the net effect for end-users).
Here are what the examples of failing-test.d.ts in this repo look like:
Before:
After:
Related to #1954 - @sheremet-va FYI. I don't think it closes it as that issue seems. to be more about how it fits into the vitest API/usage generally.
I also linked to my library in the readme.
I've opened this as a draft as I'd like to publish v0.17.0 (rather than under thenext
dist-tag). I might actually bump to v1.0.0 too, I'm thinking of this as a release candidate (candidate).Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
expectTypeOf<any>().toBeBoolean()
now produces an error, but I don't think that test needs to be duplicated here? I've checked this checkbox because I updated the error message snapshot tests.)- [ ] Please, don't make changes topnpm-lock.yaml
unless you introduce a new test example. (err I needed to update it because I was updating a dependency? Should I delete this item?)Tests
pnpm test:ci
(also updatedtest/typescript/test/runner.test.ts
and related snapshots to reflect the new error messages)Note: test/coverage-test failed with the below error but I think it's unrelated? _Edit: Yep, I got the same error on main_
Also node.spec.ts and math.ts seemed to have a newline added to them for some reason. Not sure what that's about.
Documentation
pnpm run docs
command. (_I haven't added docs, but I wrote lots in the pull request on expect-type. Is there a way to copy them here automatically and keep them in sync? Should I copy paste?)Changesets
feat:
,fix:
,perf:
,docs:
, orchore:
.