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
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 atest folder within your working directory:
test/mocks/[file-name].jstest/fixtures/[file-name].js
Writing Good Tests
Describing Tests
User Interactions
Use the@testing-library/user-event library to simulate user interactions:
Snapshot Testing
Snapshots capture component rendering and data structures: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: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
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 usingwp-env, symlink the E2E test plugins:
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:Core Block Testing
Every core block requires at least one set of fixture files for its main save function and one for each deprecation.
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 Testlabel - Tests that fail three times in a row are not considered flaky
Storybook
Storybook allows testing and developing components in isolation:trunk branch at https://wordpress.github.io/gutenberg/
Testing Best Practices
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