diff --git a/README.md b/README.md index aefcb74..a08171c 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ import { getPlaywrightConfig } from '@wpsyntex/e2e-test-utils'; export default getPlaywrightConfig( { globalSetup: './global.setup.mjs', globalTeardown: './global.teardown.mjs', + // Required with wp-env 11+ when using a separate test config file: + wpCliCommand: 'npx wp-env --config=.wp-env.test.json run cli', } ); ``` @@ -52,6 +54,7 @@ The `getPlaywrightConfig` function accepts an options object that can override a - `use`: Override default Playwright use options - `webServer`: Override default webServer configuration +- `wpCliCommand`: Base `wp-env run …` command for WP-CLI helpers such as `createTranslator`. Defaults to `npx wp-env run tests-cli`. Can also be set via the `WP_ENV_CLI_COMMAND` environment variable. - Any other Playwright configuration options #### Default Configuration @@ -194,6 +197,21 @@ Reads a file and returns its contents as a string. - `filePath`: The file path to read - **Returns:** String content of the file +#### 🔧 WP-CLI Utilities + +##### `runWpCli( command, shellPrefix = '' )` + +Runs a WP-CLI command in the wp-env test environment. Uses the command configured via `getPlaywrightConfig({ wpCliCommand })` or the `WP_ENV_CLI_COMMAND` environment variable. + +- **Parameters:** + - `command`: The WP-CLI command to run (e.g. `wp pll setting get`) + - `shellPrefix`: Optional shell prefix (e.g. `echo "y" |`) +- **Returns:** The command output as a string + +##### `getWpCliCommand()` + +Returns the base `wp-env run …` command used by `runWpCli` and `createTranslator`. Configure it via `getPlaywrightConfig({ wpCliCommand })` or the `WP_ENV_CLI_COMMAND` environment variable. + #### 🔧 Additional Exports ##### `globalSetup` diff --git a/src/config/index.js b/src/config/index.js index f263a89..60086ad 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -1,19 +1,23 @@ import { defineConfig, devices } from '@playwright/test'; import path from 'path'; +import { setWpCliCommand } from './wp-cli.js'; const STORAGE_STATE_PATH = process.env.STORAGE_STATE_PATH || path.join( process.cwd(), 'artifacts/storage-states/admin.json' ); -/** - * Returns a Playwright config object. - * - * @since 0.1.0 - * - * @param {import('@playwright/test').PlaywrightTestConfig} options Options to merge with the default config. - * @return {import('@playwright/test').PlaywrightTestConfig} Playwright config object. - */ export function getPlaywrightConfig( options = {} ) { + const { + wpCliCommand, + use: useOptions = {}, + webServer: webServerOptions = {}, + ...playwrightOptions + } = options; + + if ( wpCliCommand ) { + setWpCliCommand( wpCliCommand ); + } + return defineConfig( { testDir: './specs', globalSetup: path.resolve( __dirname, '../setup/global.setup.cjs' ), @@ -28,7 +32,7 @@ export function getPlaywrightConfig( options = {} ) { trace: 'on-first-retry', headless: true, storageState: STORAGE_STATE_PATH, - ...options.use, + ...useOptions, }, projects: [ { @@ -43,8 +47,8 @@ export function getPlaywrightConfig( options = {} ) { reuseExistingServer: true, cwd: process.cwd(), timeout: 120 * 1000, - ...options.webServer, + ...webServerOptions, }, - ...options, + ...playwrightOptions, } ); } diff --git a/src/config/wp-cli.js b/src/config/wp-cli.js new file mode 100644 index 0000000..d6c8a1d --- /dev/null +++ b/src/config/wp-cli.js @@ -0,0 +1,51 @@ +import { execSync } from 'child_process'; + +/** @type {string} */ +const DEFAULT_WP_CLI_COMMAND = 'npx wp-env run tests-cli'; + +/** + * Returns the base `wp-env run …` command used to invoke WP-CLI. + * + * Resolution order: + * 1. `process.env.WP_ENV_CLI_COMMAND` + * 2. {@link DEFAULT_WP_CLI_COMMAND} + * + * @since 0.2.0 + * + * @return {string} Base wp-env run command (without trailing WP-CLI arguments). + */ +export function getWpCliCommand() { + return process.env.WP_ENV_CLI_COMMAND || DEFAULT_WP_CLI_COMMAND; +} + +/** + * Sets the base `wp-env run …` command for utility functions such as `createTranslator`. + * + * @since 0.2.0 + * + * @param {string} command Base wp-env run command (without trailing WP-CLI arguments). + */ +export function setWpCliCommand( command ) { + process.env.WP_ENV_CLI_COMMAND = command; +} + +/** + * Runs a WP-CLI command in the wp-env test environment. + * + * @since 0.2.0 + * + * @param {string} command The WP-CLI command to run (e.g. `wp pll setting get`). + * @param {string} shellPrefix Optional shell prefix (e.g. `echo "y" |`). + * @return {string} The command output. + */ +export function runWpCli( command, shellPrefix = '' ) { + const cliCommand = `${ getWpCliCommand() } ${ command }`; + const fullCommand = shellPrefix + ? `${ shellPrefix } ${ cliCommand }` + : cliCommand; + + return execSync( fullCommand, { + encoding: 'utf8', + stdio: 'pipe', + } ); +} diff --git a/src/index.js b/src/index.js index 6cb29d8..277d05f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ export { getPlaywrightConfig } from './config/index.js'; +export { getWpCliCommand, runWpCli } from './config/wp-cli.js'; export { default as globalSetup } from './setup/global.setup.js'; export { getAllLanguages, diff --git a/src/users/index.js b/src/users/index.js index 8d99e4e..b3b072c 100644 --- a/src/users/index.js +++ b/src/users/index.js @@ -1,5 +1,5 @@ // @ts-check -import { execSync } from 'child_process'; +import { runWpCli } from '../config/wp-cli.js'; /** * @typedef {import('@playwright/test').Page} Page @@ -25,18 +25,14 @@ export async function createTranslator( langSlugs, userName = '' ) { : userName; const email = `${ userName.toLowerCase() }@example.com`; const userId = parseInt( - execSync( - `npx wp-env run tests-cli wp user create ${ userName } ${ email } --role=editor --user_pass=password --porcelain`, - { encoding: 'utf8' } + runWpCli( + `wp user create ${ userName } ${ email } --role=editor --user_pass=password --porcelain` ).trim(), 10 ); langSlugs.forEach( ( langSlug ) => { - execSync( - `npx wp-env run tests-cli wp user add-cap ${ userId } translate_${ langSlug }`, - { encoding: 'utf8' } - ); + runWpCli( `wp user add-cap ${ userId } translate_${ langSlug }` ); } ); return { id: userId, username: userName, password: 'password' };