Skip to content

Commit f6666fe

Browse files
authored
E2E tests (#220)
This PR adds: 1. A job to run WordPress E2E tests with SQLite. 2. A job to run any other custom E2E tests, reusing the WordPress setup. 3. An E2E test for the Query Monitor plugin. In the future, this can serve as the basis for more E2E tests, more 3rd-party plugins, etc.
1 parent d15f1d8 commit f6666fe

File tree

9 files changed

+176
-13
lines changed

9 files changed

+176
-13
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: End-to-end Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
name: End-to-end Tests
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 20
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v3
21+
22+
- name: Set UID and GID for PHP in WordPress images
23+
run: |
24+
echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV
25+
echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV
26+
27+
- name: Install Playwright browsers
28+
run: npx playwright install --with-deps
29+
30+
- name: Run end-to-end tests
31+
run: composer run test-e2e
32+
33+
- name: Stop Docker containers
34+
if: always()
35+
run: composer run wp-test-clean
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: WordPress End-to-end Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
name: WordPress End-to-end Tests
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 20
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v3
21+
22+
- name: Set UID and GID for PHP in WordPress images
23+
run: |
24+
echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV
25+
echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV
26+
27+
- name: Install Playwright browsers
28+
run: npx playwright install --with-deps
29+
30+
- name: Run WordPress end-to-end tests
31+
run: composer run wp-test-e2e
32+
33+
- name: Stop Docker containers
34+
if: always()
35+
run: composer run wp-test-clean

.github/workflows/wp-tests-phpunit-run.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Wrap the "composer run wp-tests-phpunit" command to process tests
2+
* Wrap the "composer run wp-tests-php" command to process tests
33
* that are expected to error and fail at the moment.
44
*
55
* This makes sure that the CI job passes, while explicitly tracking
@@ -108,7 +108,7 @@ console.log( 'Expected failures:', expectedFailures );
108108
try {
109109
try {
110110
execSync(
111-
`composer run wp-test-phpunit -- --log-junit=phpunit-results.xml --verbose`,
111+
`composer run wp-test-php -- --log-junit=phpunit-results.xml --verbose`,
112112
{ stdio: 'inherit' }
113113
);
114114
console.log( '\n⚠️ All tests passed, checking if expected errors/failures occurred...' );

.github/workflows/wp-tests-phpunit.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: WordPress Tests
1+
name: WordPress PHPUnit Tests
22

33
on:
44
push:
@@ -24,12 +24,6 @@ jobs:
2424
echo "PHP_FPM_UID=$(id -u)" >> $GITHUB_ENV
2525
echo "PHP_FPM_GID=$(id -g)" >> $GITHUB_ENV
2626
27-
- name: Setup WordPress test environment
28-
run: composer run wp-setup
29-
30-
- name: Start WordPress test environment
31-
run: composer run wp-test-start
32-
3327
- name: Run WordPress PHPUnit tests
3428
run: node .github/workflows/wp-tests-phpunit-run.js
3529

composer.json

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
"test": [
4040
"phpunit"
4141
],
42+
"test-e2e": [
43+
"@wp-test-ensure-env @no_additional_args",
44+
"rm -f tests/e2e/package.json tests/e2e/node_modules @no_additional_args",
45+
"ln -s ../../wordpress/package.json tests/e2e/package.json @no_additional_args",
46+
"ln -s ../../wordpress/node_modules tests/e2e/node_modules @no_additional_args",
47+
"npm --prefix tests/e2e run test:e2e -- --config . @additional_args"
48+
],
4249
"wp-setup": [
4350
"./wp-setup.sh"
4451
],
@@ -47,13 +54,27 @@
4754
],
4855
"wp-test-start": [
4956
"npm --prefix wordpress run env:start",
50-
"npm --prefix wordpress run env:install"
57+
"npm --prefix wordpress run env:install",
58+
"npm --prefix wordpress run env:cli -- plugin install gutenberg",
59+
"npm --prefix wordpress run env:cli -- plugin install query-monitor"
60+
],
61+
"wp-test-ensure-env": [
62+
"if [ ! -f wordpress/src/wp-load.php ]; then composer run wp-setup; fi",
63+
"@putenv COMPOSE_IGNORE_ORPHANS=true",
64+
"cd wordpress && if [ -z \"$(node tools/local-env/scripts/docker.js ps -q)\" ]; then cd ..; composer run wp-test-start; fi"
65+
],
66+
"wp-test-php": [
67+
"@wp-test-ensure-env @no_additional_args",
68+
"rm -rf wordpress/src/wp-content/database/.ht.sqlite @no_additional_args",
69+
"npm --prefix wordpress run test:php -- @additional_args"
5170
],
52-
"wp-test-phpunit": [
53-
"rm -rf wordpress/src/wp-content/database && npm --prefix wordpress run test:php --"
71+
"wp-test-e2e": [
72+
"@wp-test-ensure-env @no_additional_args",
73+
"npm --prefix wordpress run test:e2e -- @additional_args"
5474
],
5575
"wp-test-clean": [
56-
"npm --prefix wordpress run env:clean"
76+
"npm --prefix wordpress run env:clean",
77+
"rm -rf wordpress/src/wp-content/database/.ht.sqlite"
5778
]
5879
}
5980
}

tests/e2e/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/artifacts
2+
/node_modules
3+
/package.json

tests/e2e/playwright.config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import path from 'node:path';
5+
import { defineConfig } from '@playwright/test';
6+
7+
/**
8+
* WordPress dependencies
9+
*/
10+
const baseConfig = require( '@wordpress/scripts/config/playwright.config' );
11+
12+
process.env.WP_ARTIFACTS_PATH ??= path.join( process.cwd(), 'artifacts' );
13+
process.env.STORAGE_STATE_PATH ??= path.join(
14+
process.env.WP_ARTIFACTS_PATH,
15+
'storage-states/admin.json'
16+
);
17+
18+
const config = defineConfig( {
19+
...baseConfig,
20+
globalSetup: require.resolve( '../../wordpress/tests/e2e/config/global-setup.js' ),
21+
webServer: {
22+
...baseConfig.webServer,
23+
command: 'npm run env:start',
24+
},
25+
} );
26+
27+
export default config;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { test, expect } from '@wordpress/e2e-test-utils-playwright';
5+
6+
test.describe( 'Query Monitor plugin', () => {
7+
async function deactivateQueryMonitor( requestUtils ) {
8+
await requestUtils.deactivatePlugin( 'query-monitor' );
9+
const plugin = await requestUtils.rest( {
10+
path: 'wp/v2/plugins/query-monitor/query-monitor',
11+
} );
12+
expect( plugin.status ).toBe( 'inactive' );
13+
}
14+
15+
test.beforeEach( async ( { requestUtils }) => {
16+
await deactivateQueryMonitor( requestUtils );
17+
} );
18+
19+
test.afterEach( async ( { requestUtils }) => {
20+
await deactivateQueryMonitor( requestUtils );
21+
} );
22+
23+
test( 'should activate', async ( { admin, page } ) => {
24+
// Activate the Query Monitor plugin on the plugins page.
25+
await admin.visitAdminPage( '/plugins.php' );
26+
await page.getByLabel( 'Activate Query Monitor', { exact: true } ).click();
27+
await page.getByText( 'Plugin activated.', { exact: true } ).waitFor();
28+
29+
// Click on the Query Monitor menu item in the WordPress admin bar.
30+
await page.locator('a[role="menuitem"][href="#qm-overview"][aria-expanded="false"]').click();
31+
32+
// Wait for the Query Monitor panel to open.
33+
await page.locator( '#query-monitor-main' ).waitFor();
34+
await page.getByRole( 'heading', { name: 'Query Monitor', exact: true } ).waitFor();
35+
36+
// Click on the Database Queries tab.
37+
await page.getByRole( 'tab', { name: 'Database Queries' } ).click();
38+
39+
// Verify the first logged query.
40+
const sqlCell = page.locator( '.qm-row-sql' ).first();
41+
await expect( sqlCell ).toContainText( 'SELECT option_name, option_value' );
42+
43+
// Check that the query is logged with SQLite information.
44+
await sqlCell.getByLabel( 'Toggle SQLite queries' ).click();
45+
expect( page.locator('.qm-sqlite-query', { hasText: 'SELECT `option_name` , `option_value` FROM `wp_options`' }).first() ).toBeVisible();
46+
} );
47+
} );

wp-setup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ sed -i.bak "s#class WpdbExposedMethodsForTesting extends wpdb {#class WpdbExpose
6565
# 6. Install dependencies.
6666
echo "Installing dependencies..."
6767
npm --prefix "$WP_DIR" install
68+
npm --prefix "$WP_DIR" run build:dev

0 commit comments

Comments
 (0)