Just as JavaScript has become an integral part of WordPress, testing is an integral part of JavaScript development to ensure our application works how it’s supposed to—and also that it doesn’t work how it’s not supposed to!

We’ll go through the aspects of setting up everything to test our JavaScript in WordPress and learn to test JavaScript deeply:

  • we’ll use Jest to run the tests, make assertions, and mock and spy on functions
  • we’ll deal with the global variables written by WP with wp_add_inline_script or wp_localize_script and see how to integrate them in our tests
  • we’ll learn how to mock DOM methods like document.querySelector and spy on them

In general, after this, you’ll have a good understanding of start writing your own tests for your JavaScript code in your WordPress plugin or theme.