Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest how to assert that function is not called

Tags:

jestjs

In Jest there are functions like tobeCalled or toBeCalledWith to check if a particular function is called. Is there any way to check that a function is not called?

like image 918
Sachin Avatar asked Dec 08 '17 00:12

Sachin


2 Answers

Just use not.

expect(mockFn).not.toHaveBeenCalled() 

See the jest documentation

like image 101
zer0chain Avatar answered Sep 20 '22 03:09

zer0chain


not did not work for me, throwing a Invalid Chai property: toHaveBeenCalled

But using toHaveBeenCalledTimes with zero does the trick:

expect(mock).toHaveBeenCalledTimes(0)

like image 25
Barnaby Avatar answered Sep 23 '22 03:09

Barnaby