Types of tests
-
PHPUnit tests for plugins and packages
-
Javascript tests for React components
-
Javascript tests for Gutenberg blocks
-
E2E tests for plugins
Refer to Monorepo docs for information on how tests are integrated into monorepo pipelines.
These tests are used to test both plugins and packages code. Depending on developer need, they could act as a unit tests by mocking any external dependencies and apis and testing units in isolation, or they could be used as integration tests, with real WordPress installation and database.
Unit tests are used to test individual units of code, such as a function or a class. They are meant to be run in isolation, without any external dependencies. They are fast and easy to write. This is a preferred approach to PHP testing, that leads to decoupled and testable code.
Refer to PHPUnit documentation for more details on how to write tests. Also, there are various examples in the repo such as here.
Note: Jetpack monorepo is using a bit different code style to one that used in documentation examples.
Integration tests are used to test code that interacts with external dependencies, such as WordPress functions, APIs, and database. They are slower than unit tests, but still much faster than E2E tests.
There are normally two reasons why you would choose integration over unit tests:
- Code is highly coupled with WordPress and it's not possible to test it in isolation.
- You want to test how the units interact with each other, to verify that public APIs work as expected.
Normally, integration tests for packages rely on various mocking solutions available:
- brain/monkey - For mocking and stubbing WordPress functions and classes.
- automattic/jetpack-test-environment is used to pull in WordPress for testing. It calls in a lightweight version of WordPress (via the WorDBless package) and provides a way to run tests in a WordPress environment. We use the jetpack-test-environment package within the monorepo to only need one install of WordPress for the entire monorepo.
There are a lot of examples on how to use these tools in the /projects/packages
folder, such as here.
Monorepo provides support for jest
as testing framework, and @testing-library/react
as a testing library for React components.
There are examples scattered through the monorepo such as connection-status-card card. Refer to documentation for more details.
The official core documentation covers quite a lot of basics and would a good starting point for anyone starting with Gutenberg tests. There are multiple types of tests one can write for a block:
validate
tests - Tests that verify expected block output.edit
tests - Tests for edit handlers.controls
tests - Tests for block controls.
Subscriptions block has a good example of how to write these types of tests. Refer to subscriptions block tests for more details.
E2E tests are browser based tests that simulate user interactions and behavior. They are used to test user flows end to end, but also could be used as more functional tests for cases where unit and integration tests are not enough. Good example is a gutenberg block tests, where we want to test how block behaves in the editor, and how it renders on the front end.
E2E documentation goes into details on how setup them and how to write your first test.