Expected One Assertion To Be Called But Received Zero Assertion Calls
I am trying to test a method using Jest... The method should return Promise.reject() . Here is the code I wrote: test('testing Invalid Response Type', () => { con
Solution 1:
I solved it by adding return statement before the block. With return statement the function will wait for catch block to finish.. and hence expect will be executed..
test('testing Invalid Response Type', () => {
const client = newDataClient();
return client.getSomeData().then(response => {
console.log("We got data: "+ response);
}).catch(e => {
console.log("in catch");
expect(e).toBeInstanceOf(IncorrectResponseTypeError);
});
expect.assertions(1);
});
Solution 2:
You need to wait for the promise to finish to check number of assertions (to reach the .catch
block).
see jest's asynchronous tutorial, specially the async/await solution. Actually, their example is almost identical to your problem.
in your example, you would do:
test('testing Invalid Response Type', async () => { // <-- making your test async! const client = newDataClient();
await client.getSomeData().then(response => { // <-- await for your function to finishconsole.log("We got data: "+ response);
}).catch(e => {
console.log("in catch");
expect(e).toBeInstanceOf(IncorrectResponseTypeError);
});
expect.assertions(1);
});
Btw, the accepted solution also works, but not suitable to multiple tests of async code
Post a Comment for "Expected One Assertion To Be Called But Received Zero Assertion Calls"