Monday, June 3, 2024
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

 Answers
4

You're close!



The description arrow function is passed to your page.$eval mock function so you can use mockFn.mock.calls to retrieve it.



Once you've retrieved it, you can call it directly to test it and get full code coverage:



test(Should return description, async () => {
expect(await checkDescription(page)).toBe(Value); // Success!
expect(page.$eval).toHaveBeenCalled(); // Success!

const description = page.$eval.mock.calls[0][1]; // <= get the description arrow function
const getAttributeMock = jest.fn(() => 'mock content');
expect(description({ getAttribute: getAttributeMock })).toBe('mock content'); // Success!
expect(getAttributeMock).toHaveBeenCalledWith('content'); // Success!
// Success! checkDescription now has full code coverage
});

[#52166] Wednesday, April 24, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckinley

Total Points: 15
Total Questions: 101
Total Answers: 94

Location: Liechtenstein
Member since Fri, Sep 11, 2020
4 Years ago
;