Reputation: 139
For example, I'm using Archiver:
archive.on('error', err => {
if (typeof callback === 'function') {
callback.call(this, err);
} else {
throw err;
}
});
These lines are uncovered according to jest. How can you emit this error? A mock?
Upvotes: 1
Views: 1111
Reputation: 22595
You can move your callbacks to other module and then export it, like:
//calbacks.js
const errorCallback = callback => err => {
if (typeof callback === 'function') {
callback.call(this, err);
} else {
throw err;
}
}
export {errorCallback} // es6 named export
Then you can import it in your main file:
import { errorCallback } from "./callbacks.js" //path should be correct, this would work if you have both files in same directory
...
archive.on('error', errorCallback(callback)) //pass callback to curried function
You can also just import it in spec
and test it:
const spy = jest.fn()
errorCallback(fn)("error")
expect(spy).toBeCalledWith("error");
and also test case when callback is not function:
expect(() => {
errorCallback("notFunction")("error")
}).toThrow()
Upvotes: 1