UNPKG

@nx/playwright

Version:

The Nx Plugin for Playwright contains executors and generators allowing your workspace to use the powerful Playwright integration testing capabilities.

78 lines (77 loc) 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.playwrightExecutor = playwrightExecutor; const devkit_1 = require("@nx/devkit"); const child_process_1 = require("child_process"); async function playwrightExecutor(options, context) { const projectRoot = context.projectGraph?.nodes?.[context?.projectName]?.data?.root; if (!projectRoot) { throw new Error(`Unable to find the Project Root for ${context.projectName}. Is it set in the project.json?`); } if (!options.skipInstall) { devkit_1.output.log({ title: 'Ensuring Playwright is installed.', bodyLines: ['use --skipInstall to skip installation.'], }); const pmc = (0, devkit_1.getPackageManagerCommand)(); (0, child_process_1.execSync)(`${pmc.exec} playwright install`, { cwd: devkit_1.workspaceRoot, stdio: 'inherit', windowsHide: false, }); } const args = createArgs(options); const p = runPlaywright(args, context.root); p.stdout.on('data', (message) => { process.stdout.write(message); }); p.stderr.on('data', (message) => { process.stderr.write(message); }); return new Promise((resolve) => { p.on('close', (code) => { resolve({ success: code === 0 }); }); }); } function createArgs(opts, exclude = ['skipInstall']) { const args = []; const { testFiles, ...rest } = opts; if (testFiles) { args.push(...testFiles); } for (const key in rest) { if (exclude.includes(key)) continue; const value = opts[key]; // NOTE: playwright doesn't accept pascalCase args, only kebab-case const arg = (0, devkit_1.names)(key).fileName; if (Array.isArray(value)) { args.push(...value.map((v) => `--${arg}=${v.trim()}`)); } else if (typeof value === 'boolean') { // NOTE: playwright don't accept --arg=false, instead just don't pass the arg. if (value) { args.push(`--${arg}`); } } else { args.push(`--${arg}=${value}`); } } return args; } function runPlaywright(args, cwd) { try { const cli = require.resolve('@playwright/test/cli'); return (0, child_process_1.fork)(cli, ['test', ...args], { stdio: ['pipe', 'pipe', 'pipe', 'ipc'], cwd, }); } catch (e) { console.error(e); throw new Error('Unable to run playwright. Is @playwright/test installed?'); } } exports.default = playwrightExecutor;