2020-04-02 13:41:06 +00:00
# End to End Testing Environment
A reusable and extendable E2E testing environment for WooCommerce extensions.
## Installation
```bash
2020-04-30 17:01:42 +00:00
npm install @woocommerce/e2e -environment --save
2020-04-07 18:09:24 +00:00
npm install jest --global
2020-04-02 13:41:06 +00:00
```
## Configuration
2020-04-30 17:01:42 +00:00
The `@woocommerce/e2e-environment` package exports configuration objects that can be consumed in JavaScript config files in your project. Additionally, it contains several files to serve as the base for a Docker container and Travis CI setup.
2020-04-02 13:41:06 +00:00
### Babel Config
2020-05-15 09:28:21 +00:00
Make sure you `npm install @babel/preset-env --save` if you have not already done so. Afterwards, extend your project's `babel.config.js` to contain the expected presets for E2E testing.
2020-04-02 13:41:06 +00:00
```js
2020-04-30 17:01:42 +00:00
const { babelConfig: e2eBabelConfig } = require( '@woocommerce/e2e-environment' );
2020-04-02 13:41:06 +00:00
module.exports = function( api ) {
api.cache( true );
return {
...e2eBabelConfig,
presets: [
...e2eBabelConfig.presets,
'@wordpress/babel-preset-default',
],
....
};
};
```
### ES Lint Config
The E2E environment uses Puppeteer for headless browser testing, which uses certain globals variables. Avoid ES Lint errors by extending the config.
```js
2020-04-30 17:01:42 +00:00
const { esLintConfig: baseConfig } = require( '@woocommerce/e2e-environment' );
2020-04-02 13:41:06 +00:00
module.exports = {
...baseConfig,
root: true,
parser: 'babel-eslint',
extends: [
...baseConfig.extends,
'wpcalypso/react',
'plugin:jsx-a11y/recommended',
],
plugins: [
...baseConfig.plugins,
'jsx-a11y',
],
env: {
...baseConfig.env,
browser: true,
node: true,
},
globals: {
...baseConfig.globals,
wp: true,
wpApiSettings: true,
wcSettings: true,
},
....
};
```
### Jest Config
The E2E environment uses Jest as a test runner. Extending the base config is needed in order for Jest to run your project's test files.
```js
const path = require( 'path' );
2020-04-30 17:01:42 +00:00
const { jestConfig: baseE2Econfig } = require( '@woocommerce/e2e-environment' );
2020-04-02 13:41:06 +00:00
module.exports = {
...baseE2Econfig,
// Specify the path of your project's E2E tests here.
roots: [ path.resolve( __dirname, '../specs' ) ],
};
```
2020-04-28 20:14:11 +00:00
**NOTE:** Your project's Jest config file is expected to found at: `tests/e2e/config/jest.config.js` .
2020-04-02 13:41:06 +00:00
### Webpack Config
The E2E environment provides a `@woocommerce/e2e-tests` alias for easy use of the WooCommerce E2E test helpers.
```js
2020-04-30 17:01:42 +00:00
const { webpackAlias: coreE2EAlias } = require( '@woocommerce/e2e-environment' );
2020-04-02 13:41:06 +00:00
module.exports = {
....
resolve: {
alias: {
...coreE2EAlias,
....
},
},
};
```
### Docker Setup
The E2E environment will look for a `docker-compose.yaml` file in your project root. This will be combined with the base Docker config in the package. This is where you'll map your local project files into the Docker container(s).
```yaml
2020-05-15 09:21:59 +00:00
version: '3.3'
2020-04-02 13:41:06 +00:00
services:
wordpress-www:
volumes:
# This path is relative to the first config file
2020-04-28 20:14:11 +00:00
# which is in node_modules/@woocommerce/e2e/env
2020-04-02 13:41:06 +00:00
- "../../../:/var/www/html/wp-content/plugins/your-project-here"
wordpress-cli:
volumes:
- "../../../:/var/www/html/wp-content/plugins/your-project-here"
```
#### Docker Container Initialization Script
2020-04-28 20:14:11 +00:00
You can provide an initialization script that will run in the WP-CLI Docker container. Place an executable file at `tests/e2e/docker/initialize.sh` in your project and it will be copied into the container and executed. While you can run any commands you wish, the intent here is to use WP-CLI to set up your testing environment. E.g.:
2020-04-02 13:41:06 +00:00
```
#!/bin/bash
echo "Initializing WooCommerce E2E"
wp plugin install woocommerce --activate
wp theme install twentynineteen --activate
wp user create customer customer@woocommercecoree2etestsuite.com --user_pass=password --role=customer --path=/var/www/html
```
2020-05-11 18:45:32 +00:00
### Additional Packages
There are a few packages you may find useful in writing tests scripts. You can see these in use in the `tests/e2e/utils` and `tests/e2e/specs` folders.
- `@wordpress/e2e-test-utils`
- `config`
2020-04-02 13:41:06 +00:00
### Travis CI
2020-04-30 20:26:20 +00:00
Add the following to the appropriate sections of your `.travis.yml` config file.
2020-04-02 13:41:06 +00:00
```yaml
version: ~> 1.0
2020-04-30 20:26:20 +00:00
include:
- name: "Core E2E Tests"
php: 7.4
2020-05-01 13:18:21 +00:00
env: WP_VERSION=latest WP_MULTISITE=0 RUN_E2E=1
2020-04-02 13:41:06 +00:00
....
2020-04-30 20:26:20 +00:00
script:
2020-05-08 17:29:02 +00:00
- npm install jest --global
- npm explore @woocommerce/e2e -environment -- npm run test:e2e
2020-04-30 20:26:20 +00:00
2020-05-08 17:29:02 +00:00
....
after_script:
- npm explore @woocommerce/e2e -environment -- npm run docker:down
2020-04-02 13:41:06 +00:00
```
2020-05-01 13:18:21 +00:00
Use `[[ ${RUN_E2E} == 1 ]]` in your bash scripts to test for the core e2e test run.
2020-04-30 20:26:20 +00:00
2020-04-02 13:41:06 +00:00
## Usage
Start Docker
```bash
2020-04-30 17:01:42 +00:00
npm explore @woocommerce/e2e -environment -- npm run docker:up
2020-04-02 13:41:06 +00:00
```
Run E2E Tests
```bash
2020-04-30 17:01:42 +00:00
npm explore @woocommerce/e2e -environment -- npm run test:e2e
npm explore @woocommerce/e2e -environment -- npm run test:e2e-dev
2020-04-02 13:41:06 +00:00
```
Stop Docker
```bash
2020-04-30 17:01:42 +00:00
npm explore @woocommerce/e2e -environment -- npm run docker:down
```
2020-05-15 09:21:29 +00:00
## Additional information
Refer to [`tests/e2e/specs` ](https://github.com/woocommerce/woocommerce/tree/master/tests/e2e/specs ) for some test examples, and [`tests/e2e` ](https://github.com/woocommerce/woocommerce/tree/master/tests/e2e ) for general information on e2e tests.