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: improve expectTypeOf error messages #4206

Merged
merged 18 commits into from Nov 10, 2023

Conversation

mmkal
Copy link
Contributor

@mmkal mmkal commented Oct 1, 2023

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:

image

After:

image

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 the next 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:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it. (There is one fix in Improve CLI error messages mmkal/expect-type#16 - 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 to pnpm-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

  • Run the tests with pnpm test:ci (also updated test/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_ image

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

  • If you introduce new functionality, document it. You can run documentation with 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

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify
Copy link

netlify bot commented Oct 1, 2023

Deploy Preview for fastidious-cascaron-4ded94 ready!

Name Link
🔨 Latest commit e308e6b
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/654e6a988b50d2000825fe1c
😎 Deploy Preview https://deploy-preview-4206--fastidious-cascaron-4ded94.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.

@sheremet-va
Copy link
Member

I also linked to my library in the readme.

You can also add a link in the docs!

I don't think it closes it as that issue seems.

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 👍🏻

@mmkal
Copy link
Contributor Author

mmkal commented Oct 2, 2023

Oh I should update these docs too: https://vitest.dev/guide/testing-types.html#reading-errors

@mmkal
Copy link
Contributor Author

mmkal commented Oct 3, 2023

@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.

mmkal added a commit to mmkal/expect-type that referenced this pull request Oct 3, 2023
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.
@mmkal
Copy link
Contributor Author

mmkal commented Oct 5, 2023

@sheremet-va loved the talk(!). I could do with some help with the pnpm lockfile when you get a minute. I ran pnpm install but CI doesn't seem to like what it generated.

@sheremet-va
Copy link
Member

I ran pnpm install but CI doesn't seem to like what it generated.

I think you have the older version installed. I run the command for you, should be fine now.

@sheremet-va sheremet-va marked this pull request as ready for review October 7, 2023 08:10
@sheremet-va sheremet-va marked this pull request as draft October 7, 2023 08:10
@sheremet-va
Copy link
Member

Do you think we can have this in Vitest 1.0?

@mmkal
Copy link
Contributor Author

mmkal commented Nov 5, 2023

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.

@sheremet-va
Copy link
Member

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?

@mmkal
Copy link
Contributor Author

mmkal commented Nov 5, 2023

Whenever it's green. I'll take another look today or tomorrow to see why it started failing. What's the timeline for v1?

@sheremet-va
Copy link
Member

I'll take another look today or tomorrow to see why it started failing.

Thank you.

What's the timeline for v1?

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.

@mmkal
Copy link
Contributor Author

mmkal commented Nov 6, 2023

@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 \r\n line endings on Windows just in case. I'm using a Mac to develop so that's pretty much all I can do I think.

Ready for review at this point, would be great to get any help getting it over the line.

@mmkal mmkal marked this pull request as ready for review November 6, 2023 14:54
@sheremet-va
Copy link
Member

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 main.

@mmkal
Copy link
Contributor Author

mmkal commented Nov 7, 2023

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 main.

@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.

@mmkal mmkal deleted the expect-type-0.17.0-1 branch March 10, 2024 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants