Obie_One
Obie_One

Reputation: 81

Shouldn't "toHaveBeenNthCalledWith" test only one instance of the mocked function

From my understanding, toHaveBeenNthCalledWith within a jest test should test only the "Nth" call with "N" being a non-zero number. My test below returns the Nth call and the one after it. In the test, I give N a value of one and it is showing two. Now I can replaced the one with a two and then it will show 2 and 3. This is causing my test to fail. Are there any suggestions to why it is behaving like this??

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 1

Views: 2361

Answers (2)

Obie_One
Obie_One

Reputation: 81

My overall problem was whitespace. Plus I had to change the way the underlining executeQuery function was being called in the actual controller. So the expect statements in the third image are actually correct. I copied the sql statement from the controller and pasted in the test. I did NOT add tabs and it worked.

Upvotes: 0

felixmosh
felixmosh

Reputation: 35503

Probably your mock is saving state between tests, this means that it "holds" previous calls, therefore, you see 4 calls.

Try to use new mock for each test, usually done by configuring the mock in a beforeEach hook.

Edit

You can use .toHaveBeenNthCalledWith which specifies which call is under test.

Upvotes: 1

Related Questions