jest version: 20.0.3
For sync method, it works in this way.
test('example', () => {
function fn() {
throw new Error('some error');
}
expect(fn).toThrowError('some error');
});
toThrowError doesn't work with promises.
test('example', async () => {
async function fn() {
throw new Error('some error');
}
await expect(fn()).rejects.toThrowError('some error');
});
I am getting an error:
expect(function).toThrowError(string)
Received value must be a function, but instead "object" was found
This works! The async function throws a function that throws an error.
test('example', async () => {
async function fn() {
const errorFunction = () => {
throw new Error('some error');
};
throw errorFunction;
}
await expect(fn()).rejects.toThrowError('some error');
});
Obiously it doesn't make sense. I can see this line in the source code.
https://github.com/facebook/jest/pull/3068/files#diff-5d7e608b44e8080e7491366117a2025fR25
It should be Promise.reject(new Error()) instead of Promise.reject(() => {throw new Error();}
The example from the docs also doesn't work
test('fetchData() rejects to be error', async () => {
const drinkOctopus = new Promise(() => {
throw new Error('yuck, octopus flavor');
});
await expect(drinkOctopus).rejects.toMatch('octopus');
});
jest version: 20.0.3
For sync method, it works in this way.
toThrowErrordoesn't work with promises.I am getting an error:
This works! The async function throws a function that throws an error.
Obiously it doesn't make sense. I can see this line in the source code.
https://github.com/facebook/jest/pull/3068/files#diff-5d7e608b44e8080e7491366117a2025fR25
It should be
Promise.reject(new Error())instead ofPromise.reject(() => {throw new Error();}The example from the docs also doesn't work