@nx/playwright
Version:
84 lines (83 loc) • 2.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.playwrightExecutor = playwrightExecutor;
const devkit_1 = require("@nx/devkit");
const child_process_1 = require("child_process");
const deprecation_1 = require("../../utils/deprecation");
async function playwrightExecutor(options, context) {
(0, deprecation_1.warnPlaywrightExecutorDeprecation)();
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: true,
});
}
const args = createArgs(options);
const env = options.cacheDir
? { ...process.env, PWTEST_CACHE_DIR: options.cacheDir }
: undefined;
const p = runPlaywright(args, context.root, env);
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', 'cacheDir']) {
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, env) {
try {
const cli = require.resolve('@playwright/test/cli');
return (0, child_process_1.fork)(cli, ['test', ...args], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
cwd,
...(env ? { env } : {}),
});
}
catch (e) {
console.error(e);
throw new Error('Unable to run playwright. Is @playwright/test installed?');
}
}
exports.default = playwrightExecutor;