Member-only story

How Jest: toHaveBeenCalled() Work?

Tek Loon
2 min readJun 5, 2020

--

Photo by Alexandru Acea on Unsplash

This post documented down my discovery about Jest Matchers toHaveBeenCalled() function.

Before we go into the details of my discovery. Let’s see how this function normally used.

Example Usage of toHaveBeenCalled()

In the code below, funcB() is calling funcA(). When we write a unit test for funcB(), we would need to ensure funcA is called as well.

Here is the example of the unit test. In the unit test below. We’re using toHaveBeenCalled() to ensure funcA() was executed when we're calling funcB().

--

--

Tek Loon
Tek Loon

Written by Tek Loon

Coder and Writer. If you enjoy my stories— support me by https://www.buymeacoffee.com/tekloon so I can keep writing articles for the community.

No responses yet