@stratawp/testing 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.d.ts +19 -0
- package/dist/config.js +51 -0
- package/dist/config.js.map +1 -0
- package/dist/global-setup.d.ts +10 -0
- package/dist/global-setup.js +29 -0
- package/dist/global-setup.js.map +1 -0
- package/package.json +11 -1
- package/playwright.config.ts +2 -83
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PlaywrightTestConfig } from '@playwright/test';
|
|
2
|
+
|
|
3
|
+
type SmokeBrowser = 'chromium' | 'firefox' | 'webkit';
|
|
4
|
+
|
|
5
|
+
interface SmokeConfigOptions {
|
|
6
|
+
/** Directory containing the smoke specs, relative to the config file. */
|
|
7
|
+
testDir: string;
|
|
8
|
+
/** Defaults to WP_BASE_URL, then the wp-env URL. */
|
|
9
|
+
baseURL?: string;
|
|
10
|
+
/** Engines to run; STRATAWP_E2E_BROWSERS overrides this. */
|
|
11
|
+
browsers?: SmokeBrowser[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* One Playwright definition shared by the monorepo and generated themes.
|
|
15
|
+
* Chromium locally for speed; Chromium + Firefox + WebKit in CI.
|
|
16
|
+
*/
|
|
17
|
+
declare function createSmokeConfig(options: SmokeConfigOptions): PlaywrightTestConfig;
|
|
18
|
+
|
|
19
|
+
export { type SmokeBrowser, type SmokeConfigOptions, createSmokeConfig };
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
import { defineConfig, devices } from "@playwright/test";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
|
|
5
|
+
// src/browsers.ts
|
|
6
|
+
var ALL_BROWSERS = ["chromium", "firefox", "webkit"];
|
|
7
|
+
function resolveBrowsers(env = process.env, explicit) {
|
|
8
|
+
const requested = env.STRATAWP_E2E_BROWSERS;
|
|
9
|
+
if (requested) {
|
|
10
|
+
const valid = requested.split(",").map((name) => name.trim()).filter((name) => ALL_BROWSERS.includes(name));
|
|
11
|
+
if (valid.length > 0) {
|
|
12
|
+
return [...new Set(valid)];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (explicit && explicit.length > 0) {
|
|
16
|
+
return explicit;
|
|
17
|
+
}
|
|
18
|
+
return env.CI ? ALL_BROWSERS : ["chromium"];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/config.ts
|
|
22
|
+
var DEVICES = {
|
|
23
|
+
chromium: devices["Desktop Chrome"],
|
|
24
|
+
firefox: devices["Desktop Firefox"],
|
|
25
|
+
webkit: devices["Desktop Safari"]
|
|
26
|
+
};
|
|
27
|
+
function createSmokeConfig(options) {
|
|
28
|
+
const baseURL = options.baseURL ?? process.env.WP_BASE_URL ?? "http://localhost:8888";
|
|
29
|
+
return defineConfig({
|
|
30
|
+
testDir: options.testDir,
|
|
31
|
+
timeout: 3e4,
|
|
32
|
+
forbidOnly: !!process.env.CI,
|
|
33
|
+
retries: process.env.CI ? 1 : 0,
|
|
34
|
+
workers: process.env.CI ? 1 : void 0,
|
|
35
|
+
reporter: process.env.CI ? [["github"], ["html", { open: "never" }]] : [["list"]],
|
|
36
|
+
globalSetup: fileURLToPath(new URL("./global-setup.js", import.meta.url)),
|
|
37
|
+
use: {
|
|
38
|
+
baseURL,
|
|
39
|
+
trace: "retain-on-failure",
|
|
40
|
+
screenshot: "only-on-failure"
|
|
41
|
+
},
|
|
42
|
+
projects: resolveBrowsers(process.env, options.browsers).map((name) => ({
|
|
43
|
+
name,
|
|
44
|
+
use: { ...DEVICES[name] }
|
|
45
|
+
}))
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
createSmokeConfig
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/config.ts","../src/browsers.ts"],"sourcesContent":["import { defineConfig, devices } from '@playwright/test'\nimport type { PlaywrightTestConfig } from '@playwright/test'\nimport { fileURLToPath } from 'node:url'\nimport { resolveBrowsers } from './browsers'\nimport type { SmokeBrowser } from './browsers'\n\nexport type { SmokeBrowser } from './browsers'\n\nexport interface SmokeConfigOptions {\n /** Directory containing the smoke specs, relative to the config file. */\n testDir: string\n /** Defaults to WP_BASE_URL, then the wp-env URL. */\n baseURL?: string\n /** Engines to run; STRATAWP_E2E_BROWSERS overrides this. */\n browsers?: SmokeBrowser[]\n}\n\nconst DEVICES = {\n chromium: devices['Desktop Chrome'],\n firefox: devices['Desktop Firefox'],\n webkit: devices['Desktop Safari'],\n} as const\n\n/**\n * One Playwright definition shared by the monorepo and generated themes.\n * Chromium locally for speed; Chromium + Firefox + WebKit in CI.\n */\nexport function createSmokeConfig(options: SmokeConfigOptions): PlaywrightTestConfig {\n const baseURL = options.baseURL ?? process.env.WP_BASE_URL ?? 'http://localhost:8888'\n\n return defineConfig({\n testDir: options.testDir,\n timeout: 30_000,\n forbidOnly: !!process.env.CI,\n retries: process.env.CI ? 1 : 0,\n workers: process.env.CI ? 1 : undefined,\n reporter: process.env.CI ? [['github'], ['html', { open: 'never' }]] : [['list']],\n globalSetup: fileURLToPath(new URL('./global-setup.js', import.meta.url)),\n use: {\n baseURL,\n trace: 'retain-on-failure',\n screenshot: 'only-on-failure',\n },\n projects: resolveBrowsers(process.env, options.browsers).map((name) => ({\n name,\n use: { ...DEVICES[name] },\n })),\n })\n}\n","export type SmokeBrowser = 'chromium' | 'firefox' | 'webkit'\n\nexport const ALL_BROWSERS: SmokeBrowser[] = ['chromium', 'firefox', 'webkit']\n\n/**\n * Which engines the smoke suite runs on.\n * Precedence: STRATAWP_E2E_BROWSERS > explicit > (all three in CI, else chromium).\n */\nexport function resolveBrowsers(\n env: NodeJS.ProcessEnv = process.env,\n explicit?: SmokeBrowser[]\n): SmokeBrowser[] {\n const requested = env.STRATAWP_E2E_BROWSERS\n if (requested) {\n const valid = requested\n .split(',')\n .map((name) => name.trim())\n .filter((name): name is SmokeBrowser => ALL_BROWSERS.includes(name as SmokeBrowser))\n if (valid.length > 0) {\n return [...new Set(valid)]\n }\n }\n if (explicit && explicit.length > 0) {\n return explicit\n }\n return env.CI ? ALL_BROWSERS : ['chromium']\n}\n"],"mappings":";AAAA,SAAS,cAAc,eAAe;AAEtC,SAAS,qBAAqB;;;ACAvB,IAAM,eAA+B,CAAC,YAAY,WAAW,QAAQ;AAMrE,SAAS,gBACd,MAAyB,QAAQ,KACjC,UACgB;AAChB,QAAM,YAAY,IAAI;AACtB,MAAI,WAAW;AACb,UAAM,QAAQ,UACX,MAAM,GAAG,EACT,IAAI,CAAC,SAAS,KAAK,KAAK,CAAC,EACzB,OAAO,CAAC,SAA+B,aAAa,SAAS,IAAoB,CAAC;AACrF,QAAI,MAAM,SAAS,GAAG;AACpB,aAAO,CAAC,GAAG,IAAI,IAAI,KAAK,CAAC;AAAA,IAC3B;AAAA,EACF;AACA,MAAI,YAAY,SAAS,SAAS,GAAG;AACnC,WAAO;AAAA,EACT;AACA,SAAO,IAAI,KAAK,eAAe,CAAC,UAAU;AAC5C;;;ADTA,IAAM,UAAU;AAAA,EACd,UAAU,QAAQ,gBAAgB;AAAA,EAClC,SAAS,QAAQ,iBAAiB;AAAA,EAClC,QAAQ,QAAQ,gBAAgB;AAClC;AAMO,SAAS,kBAAkB,SAAmD;AACnF,QAAM,UAAU,QAAQ,WAAW,QAAQ,IAAI,eAAe;AAE9D,SAAO,aAAa;AAAA,IAClB,SAAS,QAAQ;AAAA,IACjB,SAAS;AAAA,IACT,YAAY,CAAC,CAAC,QAAQ,IAAI;AAAA,IAC1B,SAAS,QAAQ,IAAI,KAAK,IAAI;AAAA,IAC9B,SAAS,QAAQ,IAAI,KAAK,IAAI;AAAA,IAC9B,UAAU,QAAQ,IAAI,KAAK,CAAC,CAAC,QAAQ,GAAG,CAAC,QAAQ,EAAE,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC;AAAA,IAChF,aAAa,cAAc,IAAI,IAAI,qBAAqB,YAAY,GAAG,CAAC;AAAA,IACxE,KAAK;AAAA,MACH;AAAA,MACA,OAAO;AAAA,MACP,YAAY;AAAA,IACd;AAAA,IACA,UAAU,gBAAgB,QAAQ,KAAK,QAAQ,QAAQ,EAAE,IAAI,CAAC,UAAU;AAAA,MACtE;AAAA,MACA,KAAK,EAAE,GAAG,QAAQ,IAAI,EAAE;AAAA,IAC1B,EAAE;AAAA,EACJ,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FullConfig } from '@playwright/test';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fail fast, once, with an actionable message if WordPress is not answering,
|
|
5
|
+
* instead of letting every test time out separately.
|
|
6
|
+
*/
|
|
7
|
+
declare function checkSiteReachable(url: string, fetchImpl?: typeof fetch): Promise<void>;
|
|
8
|
+
declare function globalSetup(config: FullConfig): Promise<void>;
|
|
9
|
+
|
|
10
|
+
export { checkSiteReachable, globalSetup as default };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// src/global-setup.ts
|
|
2
|
+
async function checkSiteReachable(url, fetchImpl = fetch) {
|
|
3
|
+
let detail;
|
|
4
|
+
try {
|
|
5
|
+
const response = await fetchImpl(url, { signal: AbortSignal.timeout(1e4) });
|
|
6
|
+
if (response.ok) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
detail = `HTTP ${response.status}`;
|
|
10
|
+
} catch (error) {
|
|
11
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
12
|
+
const code = error?.cause?.code;
|
|
13
|
+
detail = code ? `${message}, ${code}` : message;
|
|
14
|
+
}
|
|
15
|
+
throw new Error(
|
|
16
|
+
`StrataWP smoke tests: ${url} is not reachable (${detail}). Start WordPress first (for example: pnpm exec wp-env start) or set WP_BASE_URL.`
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
async function globalSetup(config) {
|
|
20
|
+
const baseURL = config.projects[0]?.use.baseURL;
|
|
21
|
+
if (baseURL) {
|
|
22
|
+
await checkSiteReachable(baseURL);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
checkSiteReachable,
|
|
27
|
+
globalSetup as default
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=global-setup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/global-setup.ts"],"sourcesContent":["import type { FullConfig } from '@playwright/test'\n\n/**\n * Fail fast, once, with an actionable message if WordPress is not answering,\n * instead of letting every test time out separately.\n */\nexport async function checkSiteReachable(\n url: string,\n fetchImpl: typeof fetch = fetch\n): Promise<void> {\n let detail: string\n try {\n const response = await fetchImpl(url, { signal: AbortSignal.timeout(10_000) })\n if (response.ok) {\n return\n }\n detail = `HTTP ${response.status}`\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error)\n const code = (error as { cause?: { code?: string } } | null)?.cause?.code\n detail = code ? `${message}, ${code}` : message\n }\n throw new Error(\n `StrataWP smoke tests: ${url} is not reachable (${detail}). ` +\n `Start WordPress first (for example: pnpm exec wp-env start) or set WP_BASE_URL.`\n )\n}\n\nexport default async function globalSetup(config: FullConfig): Promise<void> {\n const baseURL = config.projects[0]?.use.baseURL\n if (baseURL) {\n await checkSiteReachable(baseURL)\n }\n}\n"],"mappings":";AAMA,eAAsB,mBACpB,KACA,YAA0B,OACX;AACf,MAAI;AACJ,MAAI;AACF,UAAM,WAAW,MAAM,UAAU,KAAK,EAAE,QAAQ,YAAY,QAAQ,GAAM,EAAE,CAAC;AAC7E,QAAI,SAAS,IAAI;AACf;AAAA,IACF;AACA,aAAS,QAAQ,SAAS,MAAM;AAAA,EAClC,SAAS,OAAO;AACd,UAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AACrE,UAAM,OAAQ,OAAgD,OAAO;AACrE,aAAS,OAAO,GAAG,OAAO,KAAK,IAAI,KAAK;AAAA,EAC1C;AACA,QAAM,IAAI;AAAA,IACR,yBAAyB,GAAG,sBAAsB,MAAM;AAAA,EAE1D;AACF;AAEA,eAAO,YAAmC,QAAmC;AAC3E,QAAM,UAAU,OAAO,SAAS,CAAC,GAAG,IAAI;AACxC,MAAI,SAAS;AACX,UAAM,mBAAmB,OAAO;AAAA,EAClC;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stratawp/testing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Comprehensive testing utilities for StrataWP themes",
|
|
5
5
|
"author": "Jon Imms",
|
|
6
6
|
"license": "GPL-3.0-or-later",
|
|
@@ -31,6 +31,10 @@
|
|
|
31
31
|
"./playwright": {
|
|
32
32
|
"types": "./dist/playwright.d.ts",
|
|
33
33
|
"import": "./dist/playwright.js"
|
|
34
|
+
},
|
|
35
|
+
"./config": {
|
|
36
|
+
"types": "./dist/config.d.ts",
|
|
37
|
+
"import": "./dist/config.js"
|
|
34
38
|
}
|
|
35
39
|
},
|
|
36
40
|
"files": [
|
|
@@ -57,9 +61,15 @@
|
|
|
57
61
|
"typescript": "^5.3.3"
|
|
58
62
|
},
|
|
59
63
|
"peerDependencies": {
|
|
64
|
+
"@playwright/test": ">=1.40.0",
|
|
60
65
|
"react": "^18.0.0",
|
|
61
66
|
"react-dom": "^18.0.0"
|
|
62
67
|
},
|
|
68
|
+
"peerDependenciesMeta": {
|
|
69
|
+
"@playwright/test": {
|
|
70
|
+
"optional": true
|
|
71
|
+
}
|
|
72
|
+
},
|
|
63
73
|
"scripts": {
|
|
64
74
|
"dev": "tsup --watch",
|
|
65
75
|
"build": "tsup",
|
package/playwright.config.ts
CHANGED
|
@@ -1,84 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createSmokeConfig } from '@stratawp/testing/config'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
* Playwright E2E Test Configuration
|
|
5
|
-
* See https://playwright.dev/docs/test-configuration
|
|
6
|
-
*/
|
|
7
|
-
export default defineConfig({
|
|
8
|
-
testDir: './e2e',
|
|
9
|
-
|
|
10
|
-
/* Maximum time one test can run for */
|
|
11
|
-
timeout: 30 * 1000,
|
|
12
|
-
|
|
13
|
-
/* Run tests in files in parallel */
|
|
14
|
-
fullyParallel: true,
|
|
15
|
-
|
|
16
|
-
/* Fail the build on CI if you accidentally left test.only */
|
|
17
|
-
forbidOnly: !!process.env.CI,
|
|
18
|
-
|
|
19
|
-
/* Retry on CI only */
|
|
20
|
-
retries: process.env.CI ? 2 : 0,
|
|
21
|
-
|
|
22
|
-
/* Opt out of parallel tests on CI */
|
|
23
|
-
workers: process.env.CI ? 1 : undefined,
|
|
24
|
-
|
|
25
|
-
/* Reporter to use */
|
|
26
|
-
reporter: [
|
|
27
|
-
['html'],
|
|
28
|
-
['json', { outputFile: 'test-results/results.json' }],
|
|
29
|
-
['junit', { outputFile: 'test-results/junit.xml' }],
|
|
30
|
-
],
|
|
31
|
-
|
|
32
|
-
/* Shared settings for all the projects below */
|
|
33
|
-
use: {
|
|
34
|
-
/* Base URL for WordPress */
|
|
35
|
-
baseURL: process.env.WP_BASE_URL || 'http://localhost:8888',
|
|
36
|
-
|
|
37
|
-
/* Collect trace when retrying the failed test */
|
|
38
|
-
trace: 'on-first-retry',
|
|
39
|
-
|
|
40
|
-
/* Screenshot on failure */
|
|
41
|
-
screenshot: 'only-on-failure',
|
|
42
|
-
|
|
43
|
-
/* Video on failure */
|
|
44
|
-
video: 'retain-on-failure',
|
|
45
|
-
},
|
|
46
|
-
|
|
47
|
-
/* Configure projects for major browsers */
|
|
48
|
-
projects: [
|
|
49
|
-
{
|
|
50
|
-
name: 'chromium',
|
|
51
|
-
use: { ...devices['Desktop Chrome'] },
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
{
|
|
55
|
-
name: 'firefox',
|
|
56
|
-
use: { ...devices['Desktop Firefox'] },
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
{
|
|
60
|
-
name: 'webkit',
|
|
61
|
-
use: { ...devices['Desktop Safari'] },
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
/* Test against mobile viewports */
|
|
65
|
-
{
|
|
66
|
-
name: 'Mobile Chrome',
|
|
67
|
-
use: { ...devices['Pixel 5'] },
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
name: 'Mobile Safari',
|
|
71
|
-
use: { ...devices['iPhone 12'] },
|
|
72
|
-
},
|
|
73
|
-
],
|
|
74
|
-
|
|
75
|
-
/* Run your local dev server before starting the tests */
|
|
76
|
-
webServer: process.env.CI
|
|
77
|
-
? undefined
|
|
78
|
-
: {
|
|
79
|
-
command: 'npm run dev',
|
|
80
|
-
url: 'http://localhost:3000',
|
|
81
|
-
reuseExistingServer: !process.env.CI,
|
|
82
|
-
timeout: 120 * 1000,
|
|
83
|
-
},
|
|
84
|
-
})
|
|
3
|
+
export default createSmokeConfig({ testDir: './e2e' })
|