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!: add promise-based return assertions, do not auto-resolve returned promises #5749

Merged

Conversation

sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented May 17, 2024

Description

Fixes #5747
Fixes #5647

This PR removes auto-awaiting of mock.results[0].value. To make the migration easier, we are introducing several new assertions mimicking toHaveReturned*:

interface Assertions {
  toHaveResolved: () => void
  toHaveResolvedTimes: (times: number) => void
  toHaveResolvedWith: <E>(expected: E) => void
  toHaveLastResolvedWith: <E>(value: E) => void
  toHaveNthResolvedWith: <E>(nthCall: number, value: E) => void
}

I did not add simple variations (toBeResolved and so on) because they are supposed to be softly deprecated.

You can also still get resolved results in mock.settledResults[0].value:

const mock = vi.fn(() => Promise.resolve(1))
- const result = mock.results[0].value
+ const result = mock.settledResults[0].value

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

@sheremet-va sheremet-va changed the title feat: add promise-based return assertions feat!: add promise-based return assertions, do not auto-resolve return values May 31, 2024
@sheremet-va sheremet-va force-pushed the fix/add-promise-base-return-assertions branch from b05c58a to aa24099 Compare May 31, 2024 11:46
@sheremet-va sheremet-va changed the title feat!: add promise-based return assertions, do not auto-resolve return values feat!: add promise-based return assertions, do not auto-resolve returned promises May 31, 2024
@sheremet-va sheremet-va marked this pull request as ready for review May 31, 2024 12:45
@sheremet-va sheremet-va merged commit 5f71018 into vitest-dev:main May 31, 2024
16 checks passed
@sheremet-va sheremet-va deleted the fix/add-promise-base-return-assertions branch May 31, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant