Testing JavaScript in WordPress plugins – JavaScript for WP

I spoke at the JavaScript for WP conference about writing tests for the JavaScript code in our WordPress plugins. Talk was live online on July 10, 2020 at the. The code repository and the slides can be found here.

Just as JavaScript as become an integral part of WordPress, testing JavaScript is an integral part of development to ensure that our app do what’s it’s supposed to—and also that it doesn’t do how it’s not supposed to do.

The talk went through everything we need to setup and start testing JavaScript deeply:

  • we’ll use Jest to run our tests, make assertions, and mock and spy on functions
  • we’ll learn how to work with the global variables written by WordPress using wp_add_inline_script and how to integrate them in our tests
  • we’ll learn how to mock DOM methods like document.querySelector and how to spy on them
  • in general, learn how to deal with common pitfalls when testing JavaScript

All this offered a good understanding of why and how to start writing tests for JavaScript code in a WordPress theme or plugin.

GitHub repository

The companion for this talk! All the code shown is here:

https://github.com/eliorivero/wp-jest-tests

Slides for presentation

The slides for this presentation are here:

Testing JavaScript in WordPress plugins

Leave a Reply