Skip to main content
Gutenberg contains both PHP and JavaScript code, and encourages testing for both. This guide covers the testing frameworks, commands, and best practices for contributing tests to the project.

Why Test?

Tests are important because they:
  • Help ensure the application behaves as it should
  • Provide concise examples of how to use a piece of code
  • Prevent regressions and unintended bugs
  • Document expected behavior
When writing tests, consider: What behavior(s) are we testing? What errors are likely to occur? Does the test actually test what we think it does? Is it readable for other contributors?

JavaScript Testing

Gutenberg uses Jest as the test runner for JavaScript tests, along with React Testing Library for React component testing.

Running JavaScript Tests

JavaScript Linting

Linting uses ESLint for static code analysis and TypeScript’s type-checking to catch potential errors.

Test Folder Structure

Keep your tests in a test folder within your working directory:
For mocks and fixtures, use subdirectories:
  • test/mocks/[file-name].js
  • test/fixtures/[file-name].js

Writing Good Tests

Describing Tests

Describe expected behavior from a user perspective rather than explaining code internals.

User Interactions

Use the @testing-library/user-event library to simulate user interactions:
Prefer user-event over fireEvent because it dispatches events as they would happen in real browser interactions.

Snapshot Testing

Snapshots capture component rendering and data structures:
Update Snapshots:
In watch mode, press u to update snapshots when tests fail.

Integration Testing for Blocks

Integration tests allow testing block editor functionality without a full E2E setup:
Integration tests run much faster than E2E tests and cover most block UI functionality. Use E2E tests only for interactions requiring a full browser environment (file uploads, drag and drop, etc.).

Debugging JavaScript Tests

Run tests in debug mode to attach a debugger:
Then open chrome://inspect in Chrome and click “inspect” under the Remote Target section.

PHP Testing

PHP tests use PHPUnit as the testing framework.

Running PHP Tests

PHP tests require wp-env to be running.

PHP Code Standards

Gutenberg uses PHP_CodeSniffer and the WordPress Coding Standards ruleset.

Testing Prefixed Functions

Gutenberg’s build system automatically prefixes PHP functions with gutenberg_. You must test the built (prefixed) versions of functions, not the source versions.

End-to-End Testing

Gutenberg is migrating from Puppeteer to Playwright for E2E tests.
It’s recommended to write new E2E tests in Playwright whenever possible.

Running E2E Tests

E2E tests require wp-env to be running.

E2E Testing with Puppeteer (Legacy)

Testing on Alternate Environments

If not using wp-env, symlink the E2E test plugins:
Then run tests with custom configuration:

Scenario Testing

Simulate slow CPU or network conditions to isolate race conditions:

React Native Testing

Native Mobile Unit Tests

Run React Native tests on Node without native dev tools:

Debugging Native Mobile Tests

1

Start Debug Mode

2

Open Chrome DevTools

Open chrome://inspect in Chrome.
3

Connect to Process

Click “inspect” under the Remote Target section for ../../node_modules/.bin/jest.
4

Set Breakpoints

Place breakpoints or debugger; statements in your code.
5

Resume Execution

Click the “Play” button to start test execution.

Performance Testing

Performance tests measure key metrics like editor load time, typing response time, and block selection time:
Minimize background processes and avoid using your computer during performance testing to reduce external factors affecting results.

Core Block Testing

Every core block requires at least one set of fixture files for its main save function and one for each deprecation.
These fixtures test block parsing and serialization. See the integration tests fixtures readme for details.

Flaky Tests

A test is flaky when it can pass and fail across multiple retry attempts without code changes.
  • Failed tests are automatically retried up to twice on CI
  • Flaky tests are reported to GitHub issues under the [Type] Flaky Test label
  • Tests that fail three times in a row are not considered flaky

Storybook

Storybook allows testing and developing components in isolation:
View the live Storybook for the current trunk branch at https://wordpress.github.io/gutenberg/

Testing Best Practices

Setup and Teardown:
  • Use beforeEach/afterEach for test-specific setup
  • Use beforeAll/afterAll for suite-wide setup
  • Always clean up in afterEach/afterAll to prevent test pollution

Mocking Dependencies

Testing Globals

Use Jest spies to test code that calls global methods:

Complete Test Commands Reference

JavaScript

PHP

E2E

Code Quality

Next Steps

Code Contributions

Set up your development environment

Coding Guidelines

Learn the coding standards