DEV Community

Lautaro Suarez
Lautaro Suarez

Posted on

Jest 'spyOn' and typeScript

If you been coding with typescript and want to use 'spyOn' on your test you would have to type it like this.

let functionSpy: jest.SpyInstance;
Enter fullscreen mode Exit fullscreen mode

This way we are telling the that it will be a spy instance and we will not have a ts problem.

Now, we are going to see how to actually use it which is really simple.

functionTest.tsx

const FunctionTest = () => {
  const bar = () => {
  return 'test'
}
 return(
  <div></div>
)
}

export default FunctionTest
Enter fullscreen mode Exit fullscreen mode

functionTest.test.tsx

functionSpy = jest.spyOn(FunctionTest, 'bar');
Enter fullscreen mode Exit fullscreen mode

Here we can see that following this simple step we are able to spy on the function in order to call it

Hope someone found it useFul.

Lautaro.

Top comments (0)