diff --git a/lib/command/run-multiple.js b/lib/command/run-multiple.js index 237256758..d3301214d 100644 --- a/lib/command/run-multiple.js +++ b/lib/command/run-multiple.js @@ -140,6 +140,7 @@ function executeRun(runName, runConfig) { // tweaking default output directories overriddenConfig = replaceValueDeep(overriddenConfig, 'output', path.join(config.output, outputDir)) + overriddenConfig = replaceValueDeep(overriddenConfig, 'reportDir', path.join(config.output, outputDir)) overriddenConfig = replaceValueDeep(overriddenConfig, 'mochaFile', path.join(config.output, outputDir, `${browserName}_report.xml`)) // override tests configuration diff --git a/lib/container.js b/lib/container.js index 581674c28..3a00d42fb 100644 --- a/lib/container.js +++ b/lib/container.js @@ -1,6 +1,7 @@ import { globSync } from 'glob' import path from 'path' import fs from 'fs' +import { isMainThread } from 'worker_threads' import debugModule from 'debug' const debug = debugModule('codeceptjs:container') import { MetaStep } from './step.js' @@ -731,11 +732,11 @@ async function createPlugins(config, options = {}) { const runInWorker = pluginConfig.runInWorker ?? pluginConfig.runInWorkers ?? (pluginName === 'testomatio' ? false : true) const runInParent = pluginConfig.runInParent ?? pluginConfig.runInMain ?? true - if (options.child && !runInWorker) { + if (!isMainThread && !runInWorker) { continue } - if (!options.child && store.workerMode && !runInParent) { + if (isMainThread && store.workerMode && !runInParent) { continue } let module diff --git a/test/data/sandbox/codecept.multiple.plugin.runInWorkerFalse.js b/test/data/sandbox/codecept.multiple.plugin.runInWorkerFalse.js new file mode 100644 index 000000000..d423f9a57 --- /dev/null +++ b/test/data/sandbox/codecept.multiple.plugin.runInWorkerFalse.js @@ -0,0 +1,28 @@ +export const config = { + tests: './*_test.multiple.js', + timeout: 10000, + output: './output', + helpers: { + FakeDriver: { + require: '../fake_driver', + browser: 'dummy', + windowSize: 'maximize', + }, + }, + multiple: { + default: { + browsers: ['chrome', 'firefox'], + }, + }, + plugins: { + runInWorkerFalsePlugin: { + enabled: true, + runInWorker: false, + require: './plugin-runInWorkerFalse.js', + reportDir: 'output/report', + }, + }, + bootstrap: false, + mocha: {}, + name: 'sandbox', +} diff --git a/test/data/sandbox/plugin-runInWorkerFalse.js b/test/data/sandbox/plugin-runInWorkerFalse.js new file mode 100644 index 000000000..261eff1aa --- /dev/null +++ b/test/data/sandbox/plugin-runInWorkerFalse.js @@ -0,0 +1,6 @@ +export default function (config) { + process.stdout.write('plugin-runInWorkerFalse:loaded\n') + if (config.reportDir) { + process.stdout.write(`plugin-runInWorkerFalse:reportDir=${config.reportDir}\n`) + } +} diff --git a/test/runner/run_multiple_test.js b/test/runner/run_multiple_test.js index 355cf2c39..f557b42fa 100644 --- a/test/runner/run_multiple_test.js +++ b/test/runner/run_multiple_test.js @@ -199,6 +199,23 @@ describe('CodeceptJS Multiple Runner', function () { }) }) + it('should load plugins with runInWorker:false in each child process and give each its own reportDir', done => { + exec(`${runner} run-multiple --config ${codecept_dir}/codecept.multiple.plugin.runInWorkerFalse.js default`, (err, stdout) => { + // Plugin must be initialised once per forked child (chrome + firefox = 2 times). + // Regression: in 4.x the plugin was silently skipped because options.child was + // truthy in run-multiple children, same as in worker threads. + ;(stdout.match(/plugin-runInWorkerFalse:loaded/g) || []).should.have.lengthOf(2) + // reportDir must be replaced per child so each child writes its own report. + // Regression: run-multiple.js dropped the replaceValueDeep('reportDir', ...) call + // that was present in 3.x, causing all children to overwrite the same file. + const dirs = (stdout.match(/plugin-runInWorkerFalse:reportDir=(.+)/g) || []).map(m => m.split('=')[1]) + dirs.should.have.lengthOf(2) + dirs[0].should.not.equal(dirs[1]) + assert(!err) + done() + }) + }) + describe('bootstrapAll and teardownAll', () => { const _codecept_run = `run-multiple --config ${codecept_dir}` it('should be executed from async function in config', done => {