I'm currently grappling with how to properly test a function that involves calling the Array.prototype.reverse method on an array.
The issue lies in my struggle to effectively spy on the "reverse" method. I seem unable to correctly set the parameters for the jest.spyOn function.
This is a snippet resembling the actual code I aim to test:
const parentFunction = (param) => {
let someArray = [];
switch(param) {
case 1:
someArray = childFunction();
break;
default:
break;
}
return someArray;
}
const childFunction = () => {
const anotherArray = [1, 2, 3, 4];
const reversedArray = anotherArray.reverse();
return reversedArray;
}
Here's what I've come up with for my test thus far:
test("checking if the reverse function has been called", () => {
jest.spyOn(Array, "reverse"); // encountered an error in my editor
jest.spyOn(Array.prototype, "reverse"); // also led to an error in my editor
parentFunction(1);
expect(Array.reverse).toHaveBeenCalled();
expect(Array.prototype.reverse).toHaveBeenCalled();
});
In my Visual Studio Code editor, the word "reverse" is highlighted in red and displays the following error message:
No overload matches this call. Overload 1 of 4, '(object: any\[\], method: FunctionPropertyNames\<any\[\]\>): never', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'FunctionPropertyNames\<any\[\]\>'. Overload 2 of 4, '(object: any\[\], method: ConstructorPropertyNames\<any\[\]\>): never', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'ConstructorPropertyNames\<any\[\]\>'.
Could it be that I'm missing particular imports necessary for testing this function?
Any advice or suggestions would be greatly appreciated!