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-08-05 18:55:28 +00:00
The `@woocommerce/e2e-environment` package exports configuration objects that can be consumed in JavaScript config files in your project. Additionally, it includes a hosting container for running tests and includes instructions for creating your 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-09-15 17:08:19 +00:00
const { useE2EBabelConfig } = require( '@woocommerce/e2e-environment' );
2020-04-02 13:41:06 +00:00
module.exports = function( api ) {
api.cache( true );
2020-09-15 17:08:19 +00:00
return useE2EBabelConfig( {
2020-04-02 13:41:06 +00:00
presets: [
'@wordpress/babel-preset-default',
],
2020-09-15 17:08:19 +00:00
} );
2020-04-02 13:41:06 +00:00
};
```
### 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-09-15 17:08:19 +00:00
const { useE2EEsLintConfig } = require( '@woocommerce/e2e-environment' );
2020-04-02 13:41:06 +00:00
2020-09-15 17:08:19 +00:00
module.exports = useE2EEsLintConfig( {
2020-04-02 13:41:06 +00:00
root: true,
env: {
browser: true,
2020-09-15 17:08:19 +00:00
es6: true,
node: true
2020-04-02 13:41:06 +00:00
},
globals: {
wp: true,
wpApiSettings: true,
wcSettings: true,
2020-09-15 17:08:19 +00:00
es6: true
2020-04-02 13:41:06 +00:00
},
2020-09-15 17:08:19 +00:00
} );
2020-04-02 13:41:06 +00:00
```
### 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-08-05 18:55:28 +00:00
const { useE2EJestConfig } = require( '@woocommerce/e2e-environment' );
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
const jestConfig = useE2EJestConfig( {
2020-04-02 13:41:06 +00:00
roots: [ path.resolve( __dirname, '../specs' ) ],
2020-08-05 18:55:28 +00:00
} );
module.exports = jestConfig;
2020-04-02 13:41:06 +00:00
```
2020-08-05 18:55:28 +00:00
**NOTE:** Your project's Jest config file is expected to be: `tests/e2e/config/jest.config.js` .
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
### Jest Puppeteer Config
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
The test sequencer uses the following default Puppeteer configuration:
2020-04-02 13:41:06 +00:00
```js
2020-08-05 18:55:28 +00:00
// headless
puppeteerConfig = {
launch: {
// Required for the logged out and logged in tests so they don't share app state/token.
browserContext: 'incognito',
2020-04-02 13:41:06 +00:00
},
2020-08-05 18:55:28 +00:00
};
// dev mode
puppeteerConfig = {
launch: {
...jestPuppeteerConfig.launch, // @automattic/puppeteer -utils
ignoreHTTPSErrors: true,
args: [ '--window-size=1920,1080', '--user-agent=chrome' ],
devtools: true,
defaultViewport: {
width: 1280,
height: 800,
},
},
};
2020-04-02 13:41:06 +00:00
```
2020-08-05 18:55:28 +00:00
You can customize the configuration in `tests/e2e/config/jest-puppeteer.config.js`
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
```js
const { useE2EJestPuppeteerConfig } = require( '@woocommerce/e2e-environment' );
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
const puppeteerConfig = useE2EJestPuppeteerConfig( {
launch: {
headless: false,
}
} );
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
module.exports = puppeteerConfig;
2020-04-02 13:41:06 +00:00
```
2020-08-05 18:55:28 +00:00
### Jest Setup
2020-05-11 18:45:32 +00:00
2020-08-05 18:55:28 +00:00
Jest provides setup and teardown functions similar to PHPUnit. The default setup and teardown is in [`tests/e2e/env/src/setup/jest.setup.js` ](src/setup/jest.setup.js ). Additional setup and teardown functions can be added to [`tests/e2e/config/jest.setup.js` ](../config/jest.setup.js )
2020-05-11 18:45:32 +00:00
2020-08-05 18:55:28 +00:00
### Webpack Config
2020-04-30 20:26:20 +00:00
2020-08-05 18:55:28 +00:00
The E2E environment provides a `@woocommerce/e2e-utils` alias for easy use of the WooCommerce E2E test helpers.
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
```js
const { webpackAlias: coreE2EAlias } = require( '@woocommerce/e2e-environment' );
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
module.exports = {
....
resolve: {
alias: {
...coreE2EAlias,
....
},
},
};
2020-04-02 13:41:06 +00:00
```
2020-08-05 18:55:28 +00:00
### Container Setup
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
Depending on the project and testing scenario, the built in testing environment container might not be the best solution for testing. This could be local testing where there is already a testing container, a repository that isn't a plugin or theme and there are multiple folders mapped into the container, or similar. The `e2e-environment` container supports using either the built in container or an external container. See the the appropriate readme for details:
2020-04-02 13:41:06 +00:00
2020-08-05 18:55:28 +00:00
- [Built In Container ](./builtin.md )
- [External Container ](./external.md )
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.