Skip to content

Commit 7739ad9

Browse files
committed
Improve dev/test setup; Update docs; Tests: Use shared server instance to support testing plugin zip archive before publish; Run tests for framework and template system
1 parent df4eb1c commit 7739ad9

13 files changed

+548
-64
lines changed

.wp-env.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"core": "WordPress/WordPress",
33
"phpVersion": "8.2",
4-
"plugins": ["."],
54
"mappings": {
65
"wp-content/tangible": "./vendor/tangible",
76
"wp-content/plugins/advanced-custom-fields": "./vendor/tangible-dev/advanced-custom-fields",
87
"wp-content/plugins/beaver-builder-lite-version": "./vendor/tangible-dev/beaver-builder-lite-version",
98
"wp-content/plugins/elementor": "./vendor/tangible-dev/elementor",
10-
"wp-content/plugins/tangible-e2e-plugin": "./vendor/tangible/framework/env/e2e-plugin",
9+
"wp-content/plugins/tangible-loops-and-logic": ".",
1110
"wp-content/themes/empty-block-theme": "./vendor/tangible/framework/empty-block-theme"
1211
}
1312
}

blueprint.dev.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
{
1919
"step": "login",
2020
"username": "admin"
21-
},
22-
{
23-
"step": "activatePlugin",
24-
"pluginPath": "/var/www/html/wp-content/plugins/tangible-e2e-plugin"
2521
}
2622
]
2723
}

package.json

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,49 @@
77
"url": "git+ssh://git@github.com/tangibleinc/loops-and-logic.git"
88
},
99
"scripts": {
10-
"start": "wp-now start --blueprint=blueprint.dev.json",
11-
"dev": "concurrently --raw 'roll dev' 'npm run start'",
10+
"start": "wp-env start",
11+
"start:update": "wp-env start --update",
12+
"stop": "wp-env stop",
13+
"dev": "roll dev",
1214
"build": "roll build",
1315
"format": "roll format",
1416
"postinstall": "roll install",
1517
"update": "roll update",
1618
"install:dev": "roll install --dev",
1719
"update:dev": "roll update --dev",
18-
"archive": "roll archive && rm -rf publish/tangible-loops-and-logic && unzip publish/tangible-loops-and-logic.zip -d publish/ && npm run test:archive",
19-
"// Test": "",
20-
"test": "roll run tests/index.ts",
21-
"test:all": "npm run test:7.4 && npm run test:8.4 && npm run e2e",
22-
"test:7.4": "roll run tests/index.ts PHP_VERSION=7.4",
23-
"test:8.4": "roll run tests/index.ts PHP_VERSION=8.4",
24-
"test:archive": "roll run tests/index.ts TEST_ARCHIVE=1",
20+
"archive": "FOLDER=tangible-loops-and-logic; roll archive && rm -rf publish/$FOLDER && unzip publish/$FOLDER.zip -d publish/ && npm run now:test:archive",
21+
"now": "wp-now start --blueprint=blueprint.dev.json",
22+
"now:dev": "concurrently --raw 'roll dev' 'npm run now'",
23+
"now:test": "roll run tests/now/index.ts",
24+
"now:test:archive": "roll run tests/now/index.ts TEST_ARCHIVE=1",
2525
"-- Local environment --": "https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env",
2626
"env": "wp-env",
27-
"env:start": "wp-env start",
28-
"env:update": "wp-env start --update",
29-
"env:stop": "wp-env stop",
30-
"env:composer": "FOLDER=`basename $(realpath $PWD)`; wp-env run cli --env-cwd=wp-content/plugins/$FOLDER composer",
31-
"env:composer:install": "wp-env run cli sudo apk add git && npm run env:composer install",
32-
"env:composer:update": "npm run env:composer update",
33-
"env:test": "FOLDER=`basename $(realpath $PWD)`; wp-env run tests-wordpress /var/www/html/wp-content/plugins/$FOLDER/vendor/bin/phpunit --testdox -c /var/www/html/wp-content/plugins/$FOLDER/phpunit.xml --verbose",
34-
"env:test:7.4": "WP_ENV_PHP_VERSION=7.4 wp-env start && npm run env:test",
35-
"env:test:8.2": "WP_ENV_PHP_VERSION=8.2 wp-env start && npm run env:test",
27+
"composer": "FOLDER=`basename $(realpath $PWD)`; wp-env run cli --env-cwd=wp-content/plugins/$FOLDER composer",
28+
"composer:install": "wp-env run cli sudo apk add git && npm run composer install",
29+
"composer:update": "npm run composer update",
30+
"test": "FOLDER=`basename $(realpath $PWD)`; wp-env run tests-wordpress /var/www/html/wp-content/plugins/$FOLDER/vendor/bin/phpunit --testdox -c /var/www/html/wp-content/plugins/$FOLDER/phpunit.xml --verbose",
31+
"test:7.4": "WP_ENV_PHP_VERSION=7.4 wp-env start && npm run test",
32+
"test:8.2": "WP_ENV_PHP_VERSION=8.2 wp-env start && npm run test",
33+
"test:all": "npm run test:7.4 && npm run test:8.2 && npm run e2e",
3634
"env:destroy": "wp-env destroy",
3735
"env:clean": "wp-env clean all",
3836
"-- End-to-end testing --": "https://playwright.dev/",
39-
"e2e": "playwright test --config tests/playwright.config.js",
37+
"e2e": "playwright test --config playwright.config.js",
4038
"e2e:install": "playwright install chromium",
4139
"e2e:debug": "npm run e2e -- --debug",
4240
"e2e:ui": "npm run e2e -- --ui",
4341
"e2e:watch": "chokidar 'tests/e2e/**/*.js' --initial -c 'npm run e2e -- --retries=0'"
4442
},
45-
"dependencies": {
46-
},
43+
"dependencies": {},
4744
"devDependencies": {
48-
"@playwright/test": "^1.50.1",
49-
"@tangible/env": "^2025.1.31",
50-
"@tangible/now": "^2.0.4",
45+
"@playwright/test": "^1.52.0",
46+
"@tangible/now": "^3.0.2",
5147
"@tangible/roller": "^2.1.4",
52-
"@wordpress/e2e-test-utils-playwright": "^1.18.0",
53-
"@wordpress/env": "^10.18.0",
48+
"@wordpress/e2e-test-utils-playwright": "^1.22.0",
49+
"@wordpress/env": "^10.22.0",
5450
"chokidar-cli": "^3.0.0",
5551
"concurrently": "^9.1.2",
56-
"testra": "^2.1.4"
52+
"testra": "^2.1.5"
5753
},
5854
"author": "Team Tangible",
5955
"license": "MIT",

playwright.config.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Playwright configuration
3+
* Based on https://github.com/WordPress/gutenberg/blob/trunk/packages/scripts/config/playwright.config.js
4+
* @see https://playwright.dev/docs/test-configuration
5+
*/
6+
import path, { dirname } from 'node:path'
7+
import fs from 'node:fs'
8+
import { fileURLToPath } from 'url'
9+
import { defineConfig, devices } from '@playwright/test'
10+
11+
const __dirname = dirname(fileURLToPath(import.meta.url))
12+
const readJson = (f) => JSON.parse(fs.readFileSync(f, 'utf8'))
13+
14+
export default (function createConfig() {
15+
const cwd = process.cwd()
16+
const testDir = path.join(cwd, 'tests')
17+
const testMatch = 'e2e/**/*.js'
18+
const timeout = parseInt(process.env.TIMEOUT || '', 10) || 100_000 // Defaults to 100 seconds
19+
const artifactsPath = (process.env.WP_ARTIFACTS_PATH ??= path.join(
20+
cwd,
21+
'artifacts',
22+
))
23+
const storageStatePath = (process.env.STORAGE_STATE_PATH ??= path.join(
24+
artifactsPath,
25+
'storage-states',
26+
'admin.json',
27+
))
28+
29+
// https://playwright.dev/docs/test-global-setup-teardown
30+
const globalSetup = path.join(__dirname, 'playwright.setup.js')
31+
32+
let testSitePort = readJson('.wp-env.json').testsPort || 8889
33+
try {
34+
testSitePort = readJson('.wp-env.override.json').testsPort || testSitePort
35+
} catch (e) {}
36+
37+
// Env variable used by @wordpress/e2e-test-utils-playwright
38+
let testSiteUrl =
39+
process.env.WP_BASE_URL ||
40+
(process.env.WP_BASE_URL = `http://localhost:${testSitePort}`)
41+
42+
const config = {
43+
reporter: process.env.CI ? [['github']] : [['list']],
44+
forbidOnly: !!process.env.CI,
45+
fullyParallel: false,
46+
workers: 1,
47+
retries: process.env.CI ? 2 : 0,
48+
timeout,
49+
// Don't report slow test "files", as we will be running our tests in serial.
50+
reportSlowTests: null,
51+
outputDir: path.join(artifactsPath, 'test-results'),
52+
snapshotPathTemplate:
53+
'{testDir}/{testFileDir}/__snapshots__/{arg}-{projectName}{ext}',
54+
use: {
55+
baseURL: testSiteUrl,
56+
headless: true,
57+
viewport: {
58+
width: 960,
59+
height: 700,
60+
},
61+
ignoreHTTPSErrors: true,
62+
locale: 'en-US',
63+
contextOptions: {
64+
reducedMotion: 'reduce',
65+
strictSelectors: true,
66+
},
67+
storageState: storageStatePath,
68+
actionTimeout: 10_000, // 10 seconds
69+
trace: 'retain-on-failure',
70+
screenshot: 'only-on-failure',
71+
video: 'on-first-retry',
72+
},
73+
projects: [
74+
{
75+
name: 'chromium',
76+
use: { ...devices['Desktop Chrome'] },
77+
},
78+
],
79+
80+
// Custom
81+
82+
testDir,
83+
testMatch,
84+
testIgnore: ['playwright.*.js'],
85+
globalSetup,
86+
webServer: {
87+
command: `wp-env start`,
88+
url: testSiteUrl,
89+
timeout: 120_000, // 120 seconds.
90+
reuseExistingServer: true,
91+
},
92+
}
93+
94+
return defineConfig(config)
95+
})()

playwright.setup.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Based on https://github.com/WordPress/gutenberg/blob/trunk/packages/scripts/config/playwright/global-setup.js
3+
*/
4+
import { request } from '@playwright/test'
5+
import { RequestUtils } from '@wordpress/e2e-test-utils-playwright'
6+
7+
/**
8+
* @param {import('@playwright/test').FullConfig} config
9+
* @return {Promise<void>}
10+
*/
11+
async function globalSetup(config) {
12+
13+
const { storageState, baseURL } = config.projects[0].use
14+
const storageStatePath =
15+
typeof storageState === 'string' ? storageState : undefined
16+
17+
const requestContext = await request.newContext({
18+
baseURL,
19+
})
20+
21+
const requestUtils = new RequestUtils(requestContext, {
22+
storageStatePath,
23+
baseURL,
24+
})
25+
26+
// Authenticate and save the storageState to disk.
27+
await requestUtils.setupRest()
28+
29+
await requestContext.dispose()
30+
}
31+
32+
export default globalSetup

readme.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@ npm run format
4343
Start a local dev site using [`wp-now`](https://github.com/WordPress/playground-tools/blob/trunk/packages/wp-now/README.md).
4444

4545
```sh
46-
npm run start
46+
npm run now
4747
```
4848

49-
The default user is `admin` with `password`.
50-
51-
Press CTRL + C to stop.
49+
The default user is `admin` with `password`. Press CTRL + C to stop.
5250

5351
#### Dev dependencies
5452

@@ -85,7 +83,7 @@ This plugin comes with a suite of unit and integration tests.
8583
The test environment is started by running:
8684

8785
```sh
88-
npm run env:start
86+
npm run start
8987
```
9088

9189
This uses [`wp-env`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/) to quickly spin up a local dev and test environment, optionally switching between multiple PHP versions. It requires **Docker** to be installed. There are instructions available for installing Docker on [Windows](https://docs.docker.com/desktop/install/windows-install/), [macOS](https://docs.docker.com/desktop/install/mac-install/), and [Linux](https://docs.docker.com/desktop/install/linux-install/).
@@ -95,30 +93,36 @@ Visit [http://localhost:8888](http://localhost:8888) to see the dev site, and [h
9593
Before running tests, install PHPUnit as a dev dependency using Composer inside the container.
9694

9795
```sh
98-
npm run env:composer:install
96+
npm run composer:install
9997
```
10098

10199
Composer will add and remove folders in the `vendor` folder, based on `composer.json` and `composer.lock`. If you have any existing Git repositories, ensure they don't have any work in progress before running the above command.
102100

103101
Run the tests:
104102

105103
```sh
106-
npm run env:test
104+
npm run test
107105
```
108106

109107
For each PHP version:
110108

111109
```sh
112-
npm run env:test:7.4
113-
npm run env:test:8.2
110+
npm run test:7.4
111+
npm run test:8.2
114112
```
115113

116-
The version-specific commands take a while to start, but afterwards you can run `npm run env:test` to re-run tests in the same environment.
114+
The version-specific commands take a while to start, but afterwards you can run `npm run test` to re-run tests in the same environment.
115+
116+
Run tests for all PHP versions and end-to-end tests (see below).
117+
118+
```sh
119+
npm run test:all
120+
```
117121

118122
To stop the Docker process:
119123

120124
```sh
121-
npm run env:stop
125+
npm run stop
122126
```
123127

124128
To remove Docker containers, volumes, images associated with the test environment.

tests/.wp-env.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/e2e/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { describe, test, expect } from '@tangible/env/playwright'
1+
import { test, expect } from '@wordpress/e2e-test-utils-playwright'
2+
3+
const { describe } = test
24

35
/**
46
* Tests to exercise the frontend and admin features of Tangible Blocks.
@@ -23,7 +25,6 @@ describe('Admin', () => {
2325

2426
const plugins = [
2527
['Tangible Loops & Logic', 'tangible-loops-and-logic/tangible-loops-and-logic'],
26-
['Tangible E2E', 'tangible-e2e-plugin/index'],
2728
['Elementor', 'elementor/elementor'],
2829
['Beaver Builder', 'beaver-builder-lite-version/fl-builder'],
2930
]

tests/now/.wp-env.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../.wp-env.json"
3+
}

tests/index.ts renamed to tests/now/index.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, is, ok, run } from 'testra'
2-
import { getServer } from '@tangible/env'
2+
import { getServer } from './server.ts'
33

44
async function ensurePlugin({ wpx }) {
55
return wpx/* php */ `
@@ -43,7 +43,8 @@ export default run(async () => {
4343
phpVersion: process.env.PHP_VERSION || '8.2',
4444
mappings: process.env.TEST_ARCHIVE
4545
? {
46-
'wp-content/plugins/tangible-loops-and-logic': '../publish/tangible-loops-and-logic',
46+
'wp-content/plugins/tangible-loops-and-logic':
47+
'../../publish/tangible-loops-and-logic',
4748
}
4849
: {},
4950
reset: true,
@@ -54,6 +55,9 @@ export default run(async () => {
5455
test('Plugin - Activate', async () => {
5556
let result = await ensurePlugin({ wpx })
5657
is(true, result, 'activate plugin')
58+
59+
result = await wpx/* php */ `return function_exists('tangible_template');`
60+
is(true, result, 'tangible_template() exists')
5761
})
5862

5963
test('Frontend', async () => {
@@ -63,4 +67,15 @@ export default run(async () => {
6367
// console.log(result)
6468
is(200, result.status, 'returns status 200 OK')
6569
})
70+
71+
await import(`../../vendor/tangible/framework/tests/index.ts`)
72+
73+
for (const key of [
74+
'loop',
75+
'logic',
76+
'language',
77+
'admin',
78+
]) {
79+
await import(`../../vendor/tangible/template-system/tests/${key}/index.ts`)
80+
}
6681
})

0 commit comments

Comments
 (0)