jeudi 7 février 2019

How to assert an async method throwing Error using toThrow with Jest

I have seen this question which expects a Promise to work. In my case the Error is thrown before and outside a Promise.

How can I assert the error in this case? I have tried the options below.

test('Method should throw Error', async () => {

    let throwThis = async () => {
        throw new Error();
    };

    await expect(throwThis).toThrow(Error);
    await expect(throwThis).rejects.toThrow(Error);
});

Aucun commentaire:

Enregistrer un commentaire