Skip to content
Advertisement

JEST | Assert a function was called inside addEventListener callback

I have the following function:

Code to test

JavaScript

The question is: How can I assert using Jest that createAndAppendPTag was called upon a document click event ?



Jest

This is what I tried, but can’t seem to make the test pass:

JavaScript

Terminal

This results in the following:

JavaScript

Thanks in advance.

Advertisement

Answer

I ran this simplified test to check for the side effect (p element was appended to body):

main.js

JavaScript

main.test.js

JavaScript

It passed:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement