Search code examples
jestjsistanbul

How to ignore the catch clause of a promise in Jest test coverage?


In my unit tests I have one case where I get a promise from a browser API (IndexedDB) and added a catch clause to handle any error condition. For obvious reasons I cannot trigger the promise to fail in my tests. After all this is internal to the browser.

That's why I want to ignore the catch clause for code coverage. How can I do that?

Note: for try/catch blocks it is possible to write the istanbul ignore hint like this:

try {
...
} catch(reason) /* istanbul ignore next */ {
...
}

See also https://github.com/istanbuljs/istanbuljs/issues/559.

However this doesn't work with a promise.


Solution

  • The place to add the ignore hint is directly before the closure in the catch clause:

    Promise.all([promise1, promise2]).then(() => {
         ...
    }).catch(/* istanbul ignore next */(reason) => {
        ...
    });