UNPKG

@nx/detox

Version:

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

84 lines (83 loc) 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = detoxTestExecutor; const devkit_1 = require("@nx/devkit"); const internal_1 = require("@nx/devkit/internal"); const devkit_2 = require("@nx/devkit"); const path_1 = require("path"); const child_process_1 = require("child_process"); const build_impl_1 = require("../build/build.impl"); let childProcess; async function* detoxTestExecutor(options, context) { const projectRoot = context.projectsConfigurations.projects[context.projectName].root; try { if (!options.reuse && options.buildTarget) { const buildTarget = (0, devkit_1.parseTargetString)(options.buildTarget, context); const buildOptions = (0, devkit_1.readTargetOptions)(buildTarget, context); await (0, build_impl_1.runCliBuild)(context.root, projectRoot, { ...buildOptions, detoxConfiguration: options.detoxConfiguration, }); } await runCliTest(context.root, projectRoot, options); yield { success: true }; } finally { if (childProcess) { childProcess.kill(); } } } function runCliTest(workspaceRoot, projectRoot, options) { return new Promise((resolve, reject) => { childProcess = (0, child_process_1.fork)(require.resolve('detox/local-cli/cli.js'), ['test', ...createDetoxTestOptions(options)], { cwd: (0, path_1.resolve)(workspaceRoot, projectRoot), env: process.env, }); // Ensure the child process is killed when the parent exits process.on('exit', () => childProcess.kill()); process.on('SIGTERM', () => childProcess.kill()); childProcess.on('error', (err) => { reject(err); }); childProcess.on('exit', (code, signal) => { if (code === null) code = (0, internal_1.signalToCode)(signal); if (code === 0) { resolve(code); } else { reject(code); } }); }); } const nxOptions = ['buildTarget']; function createDetoxTestOptions(options) { return Object.keys(options).reduce((acc, k) => { const propertyValue = options[k]; if (nxOptions.includes(k)) { return acc; } else if (k === 'detoxConfiguration') { acc.push('--configuration', propertyValue); } else if (k === 'deviceBootArgs') { acc.push(`--device-boot-args="${propertyValue}"`); // the value must be specified after an equal sign (=) and inside quotes: https://wix.github.io/Detox/docs/cli/test } else if (k === 'appLaunchArgs') { acc.push(`--app-launch-args="${propertyValue}"`); // the value must be specified after an equal sign (=) and inside quotes: https://wix.github.io/Detox/docs/cli/test } else if (k === 'color') { // detox only accepts --no-color, not --color if (!propertyValue) { acc.push('--no-color'); } } else { const propertyName = (0, devkit_2.names)(k).fileName; // convert camelCase to kebab-case acc.push(`--${propertyName}`, propertyValue); } return acc; }, []); }