171
rated 0 times
[
172]
[
1]
/ answers: 1 / hits: 27153
/ 5 Years ago, thu, may 2, 2019, 12:00:00
I'm trying to test a function with a callback inside. I set up a mock function, but I also need to test a callback.
I've tried to separate it as another mock function, but it doesn't counted as covered.
Function I'm trying to test:
export const checkDescription = async page => {
const metaDescription = await page.$eval(
'meta[name=description]',
description => description.getAttribute(content)
);
return metaDescription;
};
I've mocked the page function :
const page = {
$eval: jest.fn(() => Value)
};
my test :
test(Should return description, async () => {
expect(await checkDescription(page)).toBe(Value);
expect(page.$eval).toHaveBeenCalled();
});
I've tried to separate description :
const description = {
getAttribute: jest.fn(() => Value)
};
but I don't think that it's a correct way to cover description inside $eval.
More From » unit-testing