Skip to content

Commit

Permalink
rerurn failed file fix for cucumber
Browse files Browse the repository at this point in the history
  • Loading branch information
harshit-bs committed Jun 14, 2023
1 parent c1da238 commit 4cecbaa
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 72 deletions.
62 changes: 62 additions & 0 deletions lib/runner/rerunUtil.js
Original file line number Diff line number Diff line change
@@ -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
};
6 changes: 6 additions & 0 deletions lib/runner/test-runners/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
}
Expand Down
80 changes: 8 additions & 72 deletions lib/runner/test-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 4cecbaa

Please sign in to comment.