Skip to content

Conversation

samchungy
Copy link

@samchungy samchungy commented Jul 6, 2025

Description

Resolves #6279

At the moment, when you use .resolves or .rejects the type generics get lost due to the Promisify function.

import { expect } from "vitest";

interface FooBar {
  foo: string;
}

expect({ foo: "bar" }).resolves.toStrictEqual<FooBar>({ foo: "bar" });
expect({ foo: "bar" }).toStrictEqual<FooBar>({ foo: "bar" });
See error image

This PR changes the types and allows us to also type-check the resolves and reject matchers as well as properly types custom matchers as promises.

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.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

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

Copy link

netlify bot commented Jul 6, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 61ff16b
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/687516dab1c6140008d4b9e5
😎 Deploy Preview https://deploy-preview-8266--vitest-dev.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 project configuration.

@samchungy samchungy changed the title feat(types) add support for generics in promise expects feat(types): add better promise support in expects and matchers Jul 6, 2025
})

expect({ a: 1, b: '2' }).toMatchSchema({ a: '1' })
await (expect({ a: '1' }).resolves.toMatchSchema({ a: '1' }) satisfies Promise<{ a: string }>)
Copy link
Author

Choose a reason for hiding this comment

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

This previously wasn't returning a Promise.

import type { Assertion, ExpectPollOptions } from 'vitest'

declare module 'vitest' {
interface JestAssertion<T = any> extends TestingLibraryMatchers<void, T> {}
Copy link
Author

@samchungy samchungy Jul 6, 2025

Choose a reason for hiding this comment

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

Technically breaking but this aligns more with how Jest works and makes more sense?

See: https://github.com/vitest-dev/vitest/pull/8266/files#diff-810c1bb9ffac48cfe14f7dbbaf10fdf5725e06feecb99c657d49f3c7712bcd00L197-R197

image

I think we may be incorrectly setting these as it seems like the intention is for void to be R and T to be set to E here.

@samchungy
Copy link
Author

Tests seem flakey and out of control of my PR

Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

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

expect(val).resolves.eq(val) no longer returns a Promise

@samchungy
Copy link
Author

Added a test and fixed that! Thanks for catching! There's so many cases 🥲

@samchungy samchungy requested a review from sheremet-va July 15, 2025 00:29
@sheremet-va sheremet-va moved this to P2 - 5 in Team Board Jul 22, 2025
@sheremet-va sheremet-va added the p2-to-be-discussed Enhancement under consideration (priority) label Jul 22, 2025
@sheremet-va sheremet-va added this to the 5.0.0 milestone Aug 4, 2025
@sheremet-va sheremet-va moved this from P2 - 5 to Later in Team Board Aug 4, 2025
@sheremet-va sheremet-va added breaking change and removed p2-to-be-discussed Enhancement under consideration (priority) labels Aug 4, 2025
@sheremet-va
Copy link
Member

We decided that with the amount of breaking changes in Vitest 4 already, we need to postpone this at least to Vitest 5 because the impact is quite big, but it fixes a niche use case

@samchungy
Copy link
Author

Fair enough! feel free to tag me when it's time to revisit

@sheremet-va sheremet-va added the p2-nice-to-have Not breaking anything but nice to have (priority) label Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Status: Later
Development

Successfully merging this pull request may close these issues.

Missing type argument for Assertions<T> after Promisify (resolves/rejects)
2 participants