Unit Testing
Unit Testing
To ensure code reliability and maintainability, this project utilizes a dual-testing strategy: PHPUnit for backend logic and WordPress integration, and Jest for JavaScript-based components and block functionality.
Backend Testing (PHPUnit)
The PHP testing suite validates plugin logic, hooks, and database interactions. These tests require a WordPress test environment (typically provided via wp-env or a local subversion checkout).
Prerequisites
- PHPUnit: Installed via Composer.
- Subversion (svn): Required for downloading the WordPress test suite.
- Database: A dedicated test database (it will be wiped during execution).
Running PHPUnit Tests
Execute the following command from the root directory to run the full suite:
vendor/bin/phpunit
To run a specific test file or a specific test method, use the --filter flag:
# Run tests for a specific class
vendor/bin/phpunit --filter MyTestClassName
# Run a specific test method
vendor/bin/phpunit --filter test_example_method_name
Frontend Testing (Jest)
The JavaScript testing suite targets custom blocks, utility functions, and React-based UI components.
Running Jest Tests
Ensure all dependencies are installed via npm install or yarn, then run the test script defined in your package.json:
npm run test:js
Watch Mode
For an optimized development workflow, use watch mode to automatically re-run tests when files are modified:
npm run test:js -- --watch
Code Coverage
To generate a coverage report and see which parts of your JavaScript codebase are exercised by tests:
npm run test:js -- --coverage
Continuous Integration (CI)
When contributing, ensure all tests pass locally. Our CI pipeline automatically runs both suites on every Pull Request.
| Test Type | Tool | Scope | | :--- | :--- | :--- | | Backend | PHPUnit | Plugin API, Hooks, Filters, Database Models | | Frontend | Jest | Gutenberg Blocks, React Components, JS Utilities |
Best Practices for Writing Tests
- File Naming:
- PHP tests should be located in the
tests/directory and end withTest.php. - JavaScript tests should be located in
__tests__folders or end with.test.js.
- PHP tests should be located in the
- Isolation: Ensure tests do not depend on external API calls; use the provided mocks for network requests.
- Environment: Always run tests in a dedicated testing environment to prevent data loss in your development site.