From 4cecbaa096ff5e3b5152f74b09d7db7a2133bff9 Mon Sep 17 00:00:00 2001 From: Harshit Agrawal Date: Wed, 14 Jun 2023 11:54:08 +0530 Subject: [PATCH] rerurn failed file fix for cucumber --- lib/runner/rerunUtil.js | 62 ++++++++++++++++++++++ lib/runner/test-runners/cucumber.js | 6 +++ lib/runner/test-source.js | 80 +++-------------------------- 3 files changed, 76 insertions(+), 72 deletions(-) create mode 100644 lib/runner/rerunUtil.js diff --git a/lib/runner/rerunUtil.js b/lib/runner/rerunUtil.js new file mode 100644 index 0000000000..e8f7ba812f --- /dev/null +++ b/lib/runner/rerunUtil.js @@ -0,0 +1,62 @@ +const {fileExistsSync, Logger} = require('../utils'); +const path = require('path'); + +function getRerunFailedFile(minimal_report_file_path) { + const jsonFile = path.resolve(process.env.NIGHTWATCH_RERUN_REPORT_FILE || minimal_report_file_path || ''); + + if (!fileExistsSync(jsonFile)) { + const err = new Error('Unable to find the Json reporter file to rerun failed tests'); + + err.showTrace = false; + err.detailedErr = 'Configure the environment variable NIGHTWATCH_RERUN_REPORT_FILE with Json reporter file path'; + err.help = [ + `Try setting ${Logger.colors.cyan('minimal_report_file_path: "JSON-REPORTER-PATH"')} in nightwatch configuration`, + `Or, try running: ${Logger.colors.cyan('export NIGHTWATCH_RERUN_REPORT_FILE="JSON-REPORTER-PATH"')}` + ]; + + throw err; + } + + return jsonFile; +} + +function getTestSourceForRerunFailed(settings) { + const {reporter_options: {minimal_report_file_path}} = settings; + const minimalJsonFile = getRerunFailedFile(minimal_report_file_path); + + try { + const {modules = {}} = require(minimalJsonFile); + const testsource = []; + + Object.keys(modules).forEach(moduleKey => { + if (modules[moduleKey] && modules[moduleKey].status === 'fail') { + testsource.push(modules[moduleKey].modulePath); + } + }); + + if (testsource.length === 0) { + const err = new Error('Rerun Failed Tests: No failed tests found to rerun.'); + err.noFailedTestFound = true; + err.showTrace = false; + err.detailedErr = 'Run nightwatch with --help to display usage info.'; + + throw err; + } + + return testsource; + } catch (err) { + if (err.noFailedTestFound) { + err.message = 'Rerun Failed Tests: Invalid Json reporter.'; + + err.showTrace = false; + err.detailedErr = 'Please set env variable NIGHTWATCH_RERUN_REPORT_FILE with valid Json reporter path.'; + } + + throw err; + } +} + +module.exports = { + getRerunFailedFile, + getTestSourceForRerunFailed +}; diff --git a/lib/runner/test-runners/cucumber.js b/lib/runner/test-runners/cucumber.js index 7b3a4ad48a..e4b3615f01 100644 --- a/lib/runner/test-runners/cucumber.js +++ b/lib/runner/test-runners/cucumber.js @@ -3,6 +3,8 @@ const Runner = require('./default'); const TestSuite = require('../../testsuite'); const {Logger, isString, isDefined} = require('../../utils'); const {NightwatchEventHub} = require('../eventHub'); +const {getTestSourceForRerunFailed} = require('../rerunUtil'); +const Concurrency = require('../concurrency'); class CucumberSuite extends TestSuite { static isSessionCreateError(err) { @@ -102,6 +104,10 @@ class CucumberSuite extends TestSuite { const specs = this.createInitialRequires(); const {options} = this.settings.test_runner; + if ((process.env.NIGHTWATCH_RERUN_FAILED === 'true' || this.argv['rerun-failed']) && Concurrency.isMasterProcess()) { + options.feature_path = getTestSourceForRerunFailed(this.settings); + } + if (options.feature_path && !Array.isArray(options.feature_path)) { options.feature_path = [options.feature_path]; } diff --git a/lib/runner/test-source.js b/lib/runner/test-source.js index 4b504f5e95..642afeaa39 100644 --- a/lib/runner/test-source.js +++ b/lib/runner/test-source.js @@ -3,8 +3,8 @@ const fs = require('fs'); const minimatch = require('minimatch'); const Utils = require('../utils'); const Concurrency = require('./concurrency'); -const {Logger, validExtensions, tsFileExt, jsFileExt, singleSourceFile} = Utils; -const {MinimalJsonReporter} = require('../reporter/reporters/minimalJson'); +const {Logger, validExtensions, jsFileExt, singleSourceFile} = Utils; +const {getTestSourceForRerunFailed} = require('./rerunUtil'); class TestSource { static get GroupNameDelimiter() { @@ -48,83 +48,19 @@ class TestSource { return testsource; } - getRerunFailedFile(minimal_report_file_path) { - const jsonFile = path.resolve(process.env.NIGHTWATCH_RERUN_REPORT_FILE || minimal_report_file_path || ''); - - if (!Utils.fileExistsSync(jsonFile)) { - const err = new Error('Unable to find the Json reporter file to rerun failed tests'); - - err.showTrace = false; - err.detailedErr = 'Configure the environment variable NIGHTWATCH_RERUN_REPORT_FILE with Json reporter file path'; - err.help = [ - `Try setting ${Logger.colors.cyan('minimal_report_file_path: "JSON-REPORTER-PATH"')} in nightwatch configuration`, - `Or, try running: ${Logger.colors.cyan('export NIGHTWATCH_RERUN_REPORT_FILE="JSON-REPORTER-PATH"')}` - ]; - - throw err; - } - - return jsonFile; - } - - getTestSourceForRerunFailed() { - const {reporter_options: {minimal_report_file_path}} = this.settings; - const minimalJsonFile = this.getRerunFailedFile(minimal_report_file_path); - - try { - const {modules = {}} = require(minimalJsonFile); - const testsource = []; - - Object.keys(modules).forEach(moduleKey => { - if (modules[moduleKey] && modules[moduleKey].status === 'fail') { - testsource.push(modules[moduleKey].modulePath); - } - }); - - if (testsource.length === 0) { - const err = new Error('Rerun Failed Tests: No failed tests found to rerun.'); - err.noFailedTestFound = true; - err.showTrace = false; - err.detailedErr = 'Run nightwatch with --help to display usage info.'; - - throw err; - } - - return testsource; - } catch (err) { - if (err.noFailedTestFound) { - err.message = 'Rerun Failed Tests: Invalid Json reporter.'; - - err.showTrace = false; - err.detailedErr = 'Please set env variable NIGHTWATCH_RERUN_REPORT_FILE with valid Json reporter path.'; - } - - throw err; - } - } - /** * Returns the path where the tests are located * @returns {*} */ getSource() { - if ((process.env.NIGHTWATCH_RERUN_FAILED === 'true' || this.argv['rerun-failed']) && Concurrency.isMasterProcess()) { - const result = this.getTestSourceForRerunFailed(this.argv); - - const {test_runner} = this.settings; + const {test_runner} = this.settings; - if (test_runner?.type === 'cucumber') { - test_runner.options = { - ...(test_runner?.options || {}), - feature_path: result - }; + if ((process.env.NIGHTWATCH_RERUN_FAILED === 'true' || this.argv['rerun-failed']) && Concurrency.isMasterProcess() && test_runner?.type !== 'cucumber') { + return getTestSourceForRerunFailed(this.settings); + } - if (!this.src_folders && test_runner?.src_folders) { - this.src_folders = test_runner?.src_folders; - } - } else { - return result; - } + if (this.src_folders.length === 0 && test_runner?.src_folders && test_runner?.type === 'cucumber') { + this.src_folders = test_runner?.src_folders; } if (this.argv['test-worker'] || singleSourceFile(this.argv)) {