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

Async / Await expect().toThrow() does not work as expected #5538

Closed
reggi opened this issue Feb 12, 2018 · 6 comments
Closed

Async / Await expect().toThrow() does not work as expected #5538

reggi opened this issue Feb 12, 2018 · 6 comments

Comments

@reggi
Copy link

reggi commented Feb 12, 2018

I'm trying to assert that a promise throws an error with Async / Await.

async function throws () {
  throw new Error('hello world')
}

test('promise throws', async () => {
  expect(async () => {
    await throws()
  }).toThrow()
})

This is what I'm getting:

● promise throws

expect(function).toThrow(undefined)

Expected the function to throw an error.
But it didn't throw anything.

Any thoughts? Does Jest not support this? Or am I using it incorrectly?

@reggi
Copy link
Author

reggi commented Feb 12, 2018

However this passes:

async function throws () {
  throw new Error('hello world')
}

test('promise throws', async () => {
  let message = false
  try {
    await throws()
  } catch (e) {
    message = e.message
  }
  expect(message).toBeTruthy()
})

@reggi
Copy link
Author

reggi commented Feb 12, 2018

#1377

@reggi reggi closed this as completed Feb 12, 2018
@zlatinejc
Copy link

zlatinejc commented Feb 6, 2019

Basic functionality candidate.

@SimenB
Copy link
Member

SimenB commented Feb 6, 2019

What?

async function throws () {
  throw new Error('hello world')
}

test('promise throws', async () => {
  await expect(throws()).rejects.toThrow()
})

This is solved, please read the docs before commenting on old issues. https://jestjs.io/docs/en/asynchronous#resolves-rejects

@adrianjost
Copy link

adrianjost commented Nov 6, 2019

What?

async function throws () {
  throw new Error('hello world')
}

test('promise throws', async () => {
  await expect(throws()).rejects.toThrow()
})

This is solved, please read the docs before commenting on old issues. https://jestjs.io/docs/en/asynchronous#resolves-rejects

The important thing in the solution is to await the expect. Otherwise your checks will always pass.
Alternatively you could also return the expect like they do it in the docs.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants