I have a set of tests for the functions in an ES6 class that use $.get(). I was able to mock $.get(). I'm working on testing another function in the same class that uses $.get() and $(data, ownerDocument).find(), and I haven't been able to figure out how to add mocking for the $().find() function.
How I'm mocking $.get():
const jQuery = require(path/to/jquery);
jest.mock(path/to/jquery);
describe("Description", () => {
test("Test", () => {
// multiple tests with different mocks of $.get, so clearing
jQuery.get.mockClear();
jQuery.get.mockImplementation((path) => path);
// test function that uses $.get
});
});
Based on this section from the jest docs, I tried adding mocking for the jQuery constructor by using the 2 argument version of jest.mock:
// jest.mock(path/to/jquery);
jest.mock(path/to/jquery, () => {
return jest.fn().mockImplementation(arg1, arg2) => {
return {
find: () => console.log('find'); // just to see if structure works
};
});
});
describe("Description", () =>
test("Test", () => {
jQuery.get.mockClear();
jQuery.get.mockImplementation((path) => path);
// test function that uses $.get() and $(...).find()
});
});
However, adding this broke the mocking of $.get() and I started getting this error:
TypeError: cannot read property 'mockClear' of undefined
jQuery.get.mockClear()
^
Based on this article, what I believe I'm struggling with is the right way to mock functions in the $ namespace and the $.fn namespaces at the same time.
This question talks about how to mock $.ajax, and I do something like that to mock $.get but I'm not sure if it would help me mock $(...).find() as well.
Is there a way I can cleanly mock both functions I need simultaneously?
(As a side note, I'm not sure how I would create a Minimally Reproducible Example with jest, but if someone can point me to a way to do that, I'll make an MRE)