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

toThrow doesn't seem to capture the error... #5018

Closed
joetidee opened this issue Dec 5, 2017 · 6 comments
Closed

toThrow doesn't seem to capture the error... #5018

joetidee opened this issue Dec 5, 2017 · 6 comments

Comments

@joetidee
Copy link

joetidee commented Dec 5, 2017

I am doing this:

expect(async () => {await doSomething()}).toThrow('myThrownErrorMessage');

but this throws an error in the terminal:

(node:12725) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 575): Error: myThrownErrorMessage

Is this simply a case of node capturing the error before Jest does?

@SimenB
Copy link
Member

SimenB commented Dec 5, 2017

return expect(async () => {await doSomething()}).rejects.toThrow('myThrownErrorMessage'); (you might need jest@test)

https://facebook.github.io/jest/docs/en/tutorial-async.html#rejects

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions. Thank you :)

@SimenB SimenB closed this as completed Dec 5, 2017
@joetidee
Copy link
Author

joetidee commented Dec 5, 2017

I'm raising it as a bug because I would expect the toThrow function to handle the thrown error.

@SimenB
Copy link
Member

SimenB commented Dec 6, 2017

What happens if you make the surrounding it async?

@joetidee
Copy link
Author

joetidee commented Dec 6, 2017

Nothing different happens. The only way to get this to work is to do:

const mockError = new Error('someErrorMessage');
try {
    const e = await doSomething();
    expect(e).not.toEqual(mockError);
} catch (e) {
    expect(e).toEqual(mockError);
}

@magwas
Copy link

magwas commented Jul 24, 2020

This issue is easier to find than the relevant Stack overflow question: https://stackoverflow.com/questions/50807170/jest-test-fails-when-trying-to-test-an-asynchronous-function-throws
The answer of @SimenB here actually does not work. The way to make it work is to make the lambda in expect async, and put .rejects. before .toThrow

Example production code:

import fetch from "node-fetch";

export async function ConfigLoaderService():Promise<void> {
  try {
    let response = await fetch("/config.json");
  } catch(error) {
    throw new Error("Config can't be loaded: "+ error);
  }
}

example test code:

import { ConfigLoaderService } from "../../src/com.kodekonveyor.frontend/config/ConfigLoaderService";
import { enableFetchMocks } from 'jest-fetch-mock'

enableFetchMocks()

describe("ConfigLoaderService", () => {
  it("throws error if config.json not found", async () => {
    fetchMock.mockAbort()
    expect(async () => {await ConfigLoaderService()}).rejects.toThrow(Error);
  })
});

@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

3 participants