Starting in 2018, Caldera Forms began to adopt test-driven development (TDD). I wrote a post about adopting TDD based on this We are as test-driven as we can and employ several layers of testing -
- Unit tests
- Isolated tests run with Jest (JavaScript) or phpunit (PHP).
- Integration tests
- We use the WordPress "unit" test suite for these tests. This repo provides a Docker environment that they run it.
- Acceptance tests
- We use Cypress and Ghost Inspector.
All of these tools are installed and run with Composer and npm. See local dev documentation
NOTE: You should read the article on pull request workflow before developing for Caldera Forms.
When working on a bug or feature, you should follow red/green TDD flow:
- Write tests that would pass if the bug was fixed or the feature was completed.
- Push to Github and open a pull request
- Automated tests should fail.
- Travis and Github will indicate PR can not be merged with a RED light.
- Write code that makes the tests pass.
- Push to Github
- Automated tests should pass
- Travis and Github will indicate PR can not be merged with a GREEN light.
- PHP tests go in /tests and are run using phpunit
- Unit tests -- isolated tests that do NOT require WordPress -- go in
tests/Unit
.- These tests are run with
composer test:unit
. - You may NOT use any functions from WordPress core, without mocking.
- These tests are run with
- Integration tests, which require WordPress, are in
/tests
and/tests/Integration
- Tests in
tests/Integration
are newer tests, and use a PSR-4 autoloader, etc. - Tests in the main
/tests
are older. That used to be all of our tests.- For the most part, only add integration tests in
tests/Integration
- For the most part, only add integration tests in
- Tests in
- The trait
calderawp\calderaforms\Util\Traits
should have all of the factories used for integration and unit tests.- This is an aspirational goal, and partially true.
- Unit tests -- isolated tests that do NOT require WordPress -- go in
- JavaScript UNIT tests go in clients/tests
- Unit tests go in clients/tests/unit and are run using Jest
- Unit tests must have the word test in file name. For example,
formConfig.test.js
- End to end tests go in
cypress/integration
amd are written using Cypress- See our Cypress README for testing
We write our tests using Jest.
- Jest - Unit tests, assertions and test runner.
- react-test-renderer Basic React rendering for unit tests. Run by Jest.
- Enzyme - More advanced React rendendering for isolated DOM testing and other integration tests. Run by Jest.
I wrote a series of posts on testing React in context of Gutenberg for Torque:
Other recommended reading:
npm test
- Run JavaScript test watchernpm run test:once
- Run JavaScript unit tests oncecomposer test:setup
- Adds test forms and puts them on pages.
We use phpunit to run unit tests and acceptance tests.
composer test:php
- Run PHP tests -- isolated unit tests and the WordPress integration tests.composer wp:tests
- Runs the PHP integration tests using phpunit inside Docker-based environment.composer test:unit
- Run php unit tests.
- [Intro To WordPress "unit" Testing]https://torquemag.io/2017/07/practical-guide-unit-testing-code/
- Unit Testing WordPress Plugins
- [Using The WordPress Test Suite](https://torquemag.io/2018/05/advanced-oop-for-wordpress-part-5-using-the-wordpress-test-suite-for-integration-testing/
We primarily use Ghost Inspector to write and run acceptance tests against a live WordPress site. We have not yet automated this part. For Caldera Forms 1.8, we added acceptance tests with Cypress, because they are written in code and easier to automate. Also, we can run them locally.
Before running cypress test, WordPress local environment must be installed and configured:
- Install test site:
composer wp:install
- Add test forms and put on pages:
composer test:setup
- This step imports all forms in
cypress/forms
and puts them on pages. Then you can open the Cypress application:
yarn test:e2e
To add a form that gets put on a page by composer test:setup
:
- Save an export of the form file to
cypress/forms
- In
cypress/tests.json
add an object to the array with form ID (formId
) and the slug of the page (pageSlug
) you want it to appear on.
{
"formId" : "CF5bcb67b899a38",
"pageSlug": "conditionals-fancy"
},
- Cypress - Acceptance tests run against the local development environment.
- Ghost Inspector