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

fix(vitest): allow testing unandled rejection/exception #6016

Merged
merged 1 commit into from
Jul 1, 2024

Conversation

sheremet-va
Copy link
Member

Description

Fixes #5796

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 merged commit c8d56fe into vitest-dev:main Jul 1, 2024
16 checks passed
@sheremet-va sheremet-va deleted the fix/test-unhandled branch July 1, 2024 16:04
@longzheng
Copy link

Just in case anyone else comes across this looking for an example of how to test for an unhandled rejection or exception, some examples from the test file

test('can test unhandled rejection', async () => {
  const fn = vi.fn()

  const promise = new Promise<void>((resolve) => {
    process.on('unhandledRejection', () => {
      fn()
      resolve()
    })
  })

  Promise.resolve().then(() => {
    throw new Error('unhandled rejection')
  })

  await promise

  expect(fn).toHaveBeenCalledTimes(1)
})

test('can test unhandled exception', async () => {
  const fn = vi.fn()

  const promise = new Promise<void>((resolve) => {
    process.on('uncaughtException', () => {
      fn()
      resolve()
    })
  })

  nextTick(() => {
    throw new Error('unhandled exception')
  })

  await promise

  expect(fn).toHaveBeenCalledTimes(1)
})

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.

Support testing the unhandled promise rejection
2 participants