Tests

Writing tests can be boring and lazy. But we promise, cover your plugin with tests for quality - we have covered you with the complete facility and configuration.

Follow this best practices to write good and maintainable tests:

Jest

Unit- and snapshot testing

Each packages and plugins comes with a predefined and bullet-proof TypeScript implementation of Jestarrow-up-right. Simply navigate to your package or plugin and run yarn test:jestarrow-up-right. Test files are located in {packages,plugins}/*/test/jest.

If you have a look at the jest.setup.js file you will notice an inclusion of enzymearrow-up-right. Enzyme is a JavaScript library for React testing utilities - and is preinstalled ad configured in all your future packages.

Pro tip: If you write Jest tests you can start watching test files with yarn jest --watcharrow-up-right.

PHPUnit

Unit testing

Each packages and plugins comes with a predefined and bullet-proof implementation of PHPUnitarrow-up-right. Simply navigate to your package or plugin and run yarn test:phpunitarrow-up-right. Test files are located in {packages,plugins}/*/test/phpunit.

Unfortunately PHPUnit is not as handy as Jest, so we need to rely on further tools:

The boilerplate provides a TestCaseUtils trait in common/phpunit.base.php file. If you use that trait in your Test classes you can abstract util functionalities. You will find a predefined expectCallbacksReached method allowing you to expect a given callback is reached - it plays well with redefine (Patchwork):

Pro tip: If you write PHPUnit tests you can focus on single tests with yarn phpunit --filter testErrorLogWrites$arrow-up-right.

Coverage

For both Jest and PHPUnit you can collect coverage information, for this refer to yarn test:{jest|phpunit}:coveragearrow-up-right. If you commit to GitLab repository and start a merge request you will notice a Code coverage percent right to your merge request (as you can see herearrow-up-right).

Also, codecov.ioarrow-up-right is fully implemented if you just add an environment variable CODECOV_TOKEN. After you have setup Codecov you will get code coverage reports in your GitLab repository merge requests.

circle-info

Jest and PHPUnit tests coming with the boilerplate does not covered example implementations as they are removed the most time after plugin creation.

With coverage reports you will hear also about Coverage threshold. It means, if you do not reach a given percentage of coverage the pipeline will fail and you are not able to merge. The thresholds are configured in {plugins,packages}/*/package.json#phpunit-coverage-threshold and common/jest.base.js#coverageThreshold. The predefined threshold is 80.

circle-exclamation

E2E

Integration- and End-to-end testing for plugins

E2E are tests which runs directly in a (headless) browser and simulates a real user. The boilerplate comes with Cypressarrow-up-right - currently the best E2E solution we know - together with Cucumberarrow-up-right (a Gherkinarrow-up-right syntax implementation for Cypress).

You can find the cypress related files (user interactions) in plugins/your-plugin/test/cypress/. The example implementation simply logs into WordPress and checks if the Hello World REST API endpoint is available, and adds a Todo item. When opening the folder you will notice the following important abstraction:

  • 📁 integration

  • 📁 step-definitions Represents your .feature implementations

    • 📁 adminPage

      • 📄 adminPage.ts Example E2E test for the WordPress admin page

      • 📄 AdminPageObject.ts An page object describes a single page and exposes static methods. Generally you maintain all your "CSS" selectors there

    • 📁 common

      • 📄 common.ts Shareable expressions you can use in your .feature files

With GitLab CI, Cypress brings a lot of cool features to you: Do E2E tests for each commit in a complete fresh WordPress instance and make a video if something goes wrong. Prerequisite is an own GitLab runner.

Pro tip: If you write E2E tests you can focus on single features e. g. with yarn cypress-tags run -e TAGS='@only'. Refer to Running tagged testsarrow-up-right.

circle-info

Do not add configurations to cypress.josn file directly. Open plugins/your-plugin/test/cypress/plugins/index.js and have a look at the applyConfig function.

Last updated