From 628ab666c1d5c09877500d64e7c4f13ff6794ae4 Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Fri, 1 Sep 2023 17:39:52 +0530 Subject: [PATCH 1/2] use child process in mocha runner --- lib/runner/cli/cli.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/runner/cli/cli.js b/lib/runner/cli/cli.js index 6c23a2ac8e..402ce6ba16 100644 --- a/lib/runner/cli/cli.js +++ b/lib/runner/cli/cli.js @@ -24,7 +24,7 @@ class CliRunner { static get CONFIG_FILE_TS() { return './nightwatch.conf.ts'; } - + static createDefaultConfig(destFileName) { // eslint-disable-next-line no-console console.log(Logger.colors.cyan('No config file found in the current working directory, creating nightwatch.conf.js in the current folder...')); @@ -149,7 +149,7 @@ class CliRunner { } } } - + setMobileOptions(argv) { const {desiredCapabilities, selenium = {}} = this.test_settings; @@ -194,7 +194,7 @@ class CliRunner { if (!this.baseSettings) { return this; - } + } this.availableTestEnvs = Object.keys(this.baseSettings.test_settings).filter(key => { return Utils.isObject(this.baseSettings.test_settings[key]); @@ -262,7 +262,7 @@ class CliRunner { test_workers_enabled: this.test_settings.testWorkersEnabled, use_xpath: this.test_settings.use_xpath, is_bstack: this.test_settings.desiredCapabilities['bstack:options'] !== undefined, - test_runner: this.test_settings.test_runner ? this.test_settings.test_runner.type : null + test_runner: this.test_settings.test_runner ? this.test_settings.test_runner.type : null }); } @@ -297,7 +297,7 @@ class CliRunner { if (usingTS) { return path.resolve(CliRunner.CONFIG_FILE_TS); } - + return path.resolve(CliRunner.CONFIG_FILE_JS); } @@ -402,13 +402,13 @@ class CliRunner { for (const env of this.testEnvArray) { const {webdriver = {}} = this.testEnvSettings[env]; const desiredCapabilities = this.testEnvSettings[env].capabilities || this.testEnvSettings[env].desiredCapabilities; - + if (isMobile(desiredCapabilities)) { - + if (Concurrency.isWorker()) { Logger.info('Disabling parallelism while running tests on mobile platform'); } - + return false; } @@ -432,6 +432,11 @@ class CliRunner { } setupConcurrency() { + + if (this.testRunner?.type === Runner.MOCHA_RUNNER) { + this.test_settings.use_child_process = true; + } + this.concurrency = new Concurrency(this.test_settings, this.argv, this.isTestWorkersEnabled()); return this; @@ -492,7 +497,7 @@ class CliRunner { return this.runGlobalHook('before', [this.test_settings]) .then(_ => { return this.runGlobalHook('beforeChildProcess', [this.test_settings], true); - }) + }) .then(_ => { return this.getTestsFiles(); }) @@ -628,7 +633,7 @@ class CliRunner { } analyticsCollector.__flush(); - + return errorOrFailed; }); } From 2545379a8949e5ed30ebb73a551a91ec3066188f Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Fri, 1 Sep 2023 17:42:38 +0530 Subject: [PATCH 2/2] added test --- test/src/cli/testCliRunnerMocha.js | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/src/cli/testCliRunnerMocha.js b/test/src/cli/testCliRunnerMocha.js index 07c7a8f56f..0f0244c2fd 100644 --- a/test/src/cli/testCliRunnerMocha.js +++ b/test/src/cli/testCliRunnerMocha.js @@ -26,6 +26,7 @@ describe('test CLI Runner Mocha', function() { resolve: function(a) { return a; }, + extname: path.extname, join: path.join }); @@ -89,6 +90,49 @@ describe('test CLI Runner Mocha', function() { }); }); + it('testRunWithMocha - parallelism', function() { + + const ChildProcess = common.require('runner/concurrency/child-process.js'); + let childProcessCreated = false; + class ChildProcessMock extends ChildProcess { + run(colors, type) { + childProcessCreated = true; + assert.strictEqual(colors.length, 4); + assert.strictEqual(type, 'workers'); + assert.deepStrictEqual(Object.keys(this._events), ['message']); + + return Promise.resolve(0); + } + } + + mockery.registerMock('./child-process.js', ChildProcessMock); + + mockery.registerMock('./withmocha.json', { + src_folders: ['tests'], + output_folder: false, + use_child_process: false, + test_settings: { + 'default': { + silent: true + } + }, + test_runner: 'mocha' + }); + + + const CliRunner = common.require('runner/cli/cli.js'); + const runner = new CliRunner({ + config: './withmocha.json', + env: 'default', + reporter: 'junit', + parallel: true + }).setup(); + + return runner.runTests().then(function() { + assert.ok(childProcessCreated, 'mocha runner with parallel threads should use child process'); + }); + }); + it('testRunWithMochaPerEnvironment', function() { const testFiles = []; const defaultOptions = {timeout: 20000, reporterOptions: {}};