From 903ced8df19a2a097be63033a857c69f255d0875 Mon Sep 17 00:00:00 2001 From: Manoj Rajan Date: Wed, 20 Jan 2021 16:30:31 +0530 Subject: [PATCH 1/6] Rerun of Failed Tests --- bin/codecept.js | 2 ++ lib/codecept.js | 41 +++++++++++++--------- lib/command/run-workers.js | 4 +++ lib/command/run.js | 5 +++ lib/listener/exit.js | 16 +++++++++ lib/listener/steps.js | 7 ++++ lib/rerunFailed.js | 71 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 lib/rerunFailed.js diff --git a/bin/codecept.js b/bin/codecept.js index b1e0a3737..5f1f18bde 100755 --- a/bin/codecept.js +++ b/bin/codecept.js @@ -95,6 +95,7 @@ program.command('run [test]') .option('--features', 'run only *.feature files and skip tests') .option('--tests', 'run only JS test files and skip features') .option('-p, --plugins ', 'enable plugins, comma-separated') + .option('--failed','to run the failed tests') // mocha options .option('--colors', 'force enabling of colors') @@ -133,6 +134,7 @@ program.command('run-workers ') .option('-p, --plugins ', 'enable plugins, comma-separated') .option('-O, --reporter-options ', 'reporter-specific options') .option('-R, --reporter ', 'specify the reporter to use') + .option('--failed','to run the failed tests') .action(require('../lib/command/run-workers')); program.command('run-multiple [suites...]') diff --git a/lib/codecept.js b/lib/codecept.js index d355f8ef4..07c431c36 100644 --- a/lib/codecept.js +++ b/lib/codecept.js @@ -1,4 +1,5 @@ const { existsSync, readFileSync } = require('fs'); +const fs = require('fs'); const glob = require('glob'); const fsPath = require('path'); const { resolve } = require('path'); @@ -7,7 +8,8 @@ const container = require('./container'); const Config = require('./config'); const event = require('./event'); const runHook = require('./hooks'); -const output = require('./output'); +const {output,print} = require('./output'); +const FailedTestsRerun = require('./rerunFailed'); /** * CodeceptJS runner @@ -122,25 +124,32 @@ class Codecept { * * @param {string} [pattern] */ - loadTests(pattern) { + loadTests(pattern){ const options = { cwd: global.codecept_dir, }; - - let patterns = [pattern]; - if (!pattern) { - patterns = []; - if (this.config.tests && !this.opts.features) patterns.push(this.config.tests); - if (this.config.gherkin.features && !this.opts.tests) patterns.push(this.config.gherkin.features); + if(this.opts.failed){ + print('Running The Tests in Failed Re-Run Mode ..'); + const failedTestRerun = new FailedTestsRerun(); + const optionForCheck = failedTestRerun.getOptions(); + failedTestRerun.checkForFailedTestsExist(optionForCheck); + this.testFiles= JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); } - - for (pattern of patterns) { - glob.sync(pattern, options).forEach((file) => { - if (!fsPath.isAbsolute(file)) { - file = fsPath.join(global.codecept_dir, file); - } - this.testFiles.push(fsPath.resolve(file)); - }); + else { + let patterns = [pattern]; + if (!pattern) { + patterns = []; + if (this.config.tests && !this.opts.features) patterns.push(this.config.tests); + if (this.config.gherkin.features && !this.opts.tests) patterns.push(this.config.gherkin.features); + } + for (pattern of patterns) { + glob.sync(pattern, options).forEach((file) => { + if (!fsPath.isAbsolute(file)) { + file = fsPath.join(global.codecept_dir, file); + } + this.testFiles.push(fsPath.resolve(file)); + }); + } } } diff --git a/lib/command/run-workers.js b/lib/command/run-workers.js index c578c39e4..817d6d296 100644 --- a/lib/command/run-workers.js +++ b/lib/command/run-workers.js @@ -4,6 +4,8 @@ const { tryOrDefault } = require('../utils'); const output = require('../output'); const event = require('../event'); const Workers = require('../workers'); +const FailedTestRerun = require('./../rerunFailed'); +const failedTestRerun = new FailedTestRerun(); module.exports = async function (workerCount, options) { satisfyNodeVersion( @@ -23,6 +25,8 @@ module.exports = async function (workerCount, options) { options, }; + failedTestRerun.setOptions(options); + const numberOfWorkers = parseInt(workerCount, 10); output.print(`CodeceptJS v${require('../codecept').version()}`); diff --git a/lib/command/run.js b/lib/command/run.js index d70972e0e..57a488626 100644 --- a/lib/command/run.js +++ b/lib/command/run.js @@ -3,6 +3,9 @@ const { } = require('./utils'); const Config = require('../config'); const Codecept = require('../codecept'); +const FailedTestRerun = require('./../rerunFailed'); +const failedTestRerun = new FailedTestRerun(); +const {stringify} = require('flatted') module.exports = async function (test, options) { // registering options globally to use in config @@ -19,6 +22,8 @@ module.exports = async function (test, options) { createOutputDir(config, testRoot); const codecept = new Codecept(config, options); + failedTestRerun.setOptions(options); + try { codecept.init(testRoot); diff --git a/lib/listener/exit.js b/lib/listener/exit.js index 2362acc33..a9b771364 100644 --- a/lib/listener/exit.js +++ b/lib/listener/exit.js @@ -2,12 +2,18 @@ const event = require('../event'); module.exports = function () { let failedTests = []; + let failedTestName = []; + let passedTest = []; + const FailedTestRerun = require('./../rerunFailed'); + const failedTestRerun = new FailedTestRerun(); event.dispatcher.on(event.test.failed, (testOrSuite) => { // NOTE When an error happens in one of the hooks (BeforeAll/BeforeEach...) the event object // is a suite and not a test const id = testOrSuite.id || (testOrSuite.ctx && testOrSuite.ctx.test.id) || 'empty'; + const name = testOrSuite.file; failedTests.push(id); + failedTestName.push(name); }); // if test was successful after retries @@ -15,7 +21,9 @@ module.exports = function () { // NOTE When an error happens in one of the hooks (BeforeAll/BeforeEach...) the event object // is a suite and not a test const id = testOrSuite.id || (testOrSuite.ctx && testOrSuite.ctx.test.id) || 'empty'; + const name = testOrSuite.file; failedTests = failedTests.filter(failed => id !== failed); + failedTestRerun.removePassedTests(name); }); event.dispatcher.on(event.all.result, () => { @@ -24,6 +32,14 @@ module.exports = function () { } }); + event.dispatcher.on(event.all.after, () => { + + // Writes the Failed Test Names In The JSON File For Rerun + if(failedTests.length > 0) { + failedTestRerun.writeFailedTests(failedTestName); + } + }); + process.on('beforeExit', (code) => { if (code) { process.exit(code); diff --git a/lib/listener/steps.js b/lib/listener/steps.js index 660bf015b..731bbed02 100644 --- a/lib/listener/steps.js +++ b/lib/listener/steps.js @@ -3,6 +3,7 @@ const store = require('../store'); let currentTest; let currentHook; +let failedTests = new Array(0); /** * Register steps inside tests @@ -33,6 +34,12 @@ module.exports = function () { }); event.dispatcher.on(event.test.failed, () => { + //Getting Failes Test Scrips To Add in JSON File for Rerun + // @ts-ignore + let failedTestName = currentTest.file; + failedTests.push(failedTestName); + + const cutSteps = function (current) { const failureIndex = current.steps.findIndex(el => el.status === 'failed'); // To be sure that failed test will be failed in report diff --git a/lib/rerunFailed.js b/lib/rerunFailed.js new file mode 100644 index 000000000..0a1e2c064 --- /dev/null +++ b/lib/rerunFailed.js @@ -0,0 +1,71 @@ +const fs = require('fs'); +const { print } = require('codeceptjs/lib/output'); +let optionsForCheck; + +class FailedTestsRerun { + + setOptions(options) { + optionsForCheck = options; + } + + getOptions(){ + return optionsForCheck; + } + + checkForFailedTestsExist(options) { + if (options.failed) { + if (!fs.existsSync('failedCases.json')) { + print('There Are No Failed Tests In Previous Execution'); + process.exit() + } + else{ + const failedTests = this.getFailedTestContent(); + if (failedTests.length === 0 || failedTests.toString() === '') { + print('There Are No Files Present In the Failed Tests File'); + process.exit(); + } + } + } + } + + getFailedTestContent(){ + return fs.readFileSync('failedCases.json', {encoding: 'utf8'}).split(','); + } + + getFailedTests() { + return JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); + } + + writeFailedTests(failedTests) { + fs.writeFile('failedCases.json', JSON.stringify(failedTests),function (err) { + if (err) + return print(err); + }); + } + + removePassedTests(passedTest) { + if (optionsForCheck.failed ) { + let currentFile = JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); + currentFile.forEach((t) => { + if (currentFile.includes(passedTest)) { + const index = currentFile.indexOf(t); + if (index > -1) { + currentFile.splice(index, 1); + } + } + }); + if(currentFile.length === 0 ){ + fs.unlink('failedCases.json',err => { + if(err) throw err; + }); + } + else{ + fs.writeFile('failedCases.json', currentFile, function (err) { + if (err) throw err + }); + } + } + } +} + +module.exports = FailedTestsRerun; From 87bc433eea5a4480cd53542c540f82040c2ba058 Mon Sep 17 00:00:00 2001 From: Manoj Rajan Date: Thu, 21 Jan 2021 07:58:48 +0530 Subject: [PATCH 2/6] Rerun Failed Tests in Sequential and Parallel mode --- lib/codecept.js | 3 +-- lib/command/run.js | 2 -- lib/listener/steps.js | 2 +- lib/rerunFailed.js | 32 +++++++++++++++++++++++++++----- lib/workers.js | 15 +++++++++++++++ 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/lib/codecept.js b/lib/codecept.js index 07c431c36..5606d88c8 100644 --- a/lib/codecept.js +++ b/lib/codecept.js @@ -131,8 +131,7 @@ class Codecept { if(this.opts.failed){ print('Running The Tests in Failed Re-Run Mode ..'); const failedTestRerun = new FailedTestsRerun(); - const optionForCheck = failedTestRerun.getOptions(); - failedTestRerun.checkForFailedTestsExist(optionForCheck); + failedTestRerun.checkForFailedTestsExist(this.opts); this.testFiles= JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); } else { diff --git a/lib/command/run.js b/lib/command/run.js index 57a488626..2154dd75f 100644 --- a/lib/command/run.js +++ b/lib/command/run.js @@ -23,8 +23,6 @@ module.exports = async function (test, options) { const codecept = new Codecept(config, options); failedTestRerun.setOptions(options); - - try { codecept.init(testRoot); await codecept.bootstrap(); diff --git a/lib/listener/steps.js b/lib/listener/steps.js index 731bbed02..d5ffb0200 100644 --- a/lib/listener/steps.js +++ b/lib/listener/steps.js @@ -34,7 +34,7 @@ module.exports = function () { }); event.dispatcher.on(event.test.failed, () => { - //Getting Failes Test Scrips To Add in JSON File for Rerun + //Getting Failed Test Scrips To Add in JSON File for Rerun // @ts-ignore let failedTestName = currentTest.file; failedTests.push(failedTestName); diff --git a/lib/rerunFailed.js b/lib/rerunFailed.js index 0a1e2c064..f615dff75 100644 --- a/lib/rerunFailed.js +++ b/lib/rerunFailed.js @@ -4,14 +4,19 @@ let optionsForCheck; class FailedTestsRerun { + /** + * Sets The Options When The Run is Initiated + * @param options + */ setOptions(options) { optionsForCheck = options; } - getOptions(){ - return optionsForCheck; - } - + /** + * Initial Check To Find the Existence Of Failed Cases JSON File + * and Content + * @param options + */ checkForFailedTestsExist(options) { if (options.failed) { if (!fs.existsSync('failedCases.json')) { @@ -21,21 +26,33 @@ class FailedTestsRerun { else{ const failedTests = this.getFailedTestContent(); if (failedTests.length === 0 || failedTests.toString() === '') { - print('There Are No Files Present In the Failed Tests File'); + print('There Are No Files Present In the Failed Cases Json File'); process.exit(); } } } } + /** + * Returns The Content In JSON File For File/Content existence Check + * @return {string[]} + */ getFailedTestContent(){ return fs.readFileSync('failedCases.json', {encoding: 'utf8'}).split(','); } + /** + * Returns The Failed Tests + * @return {any} + */ getFailedTests() { return JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); } + /** + * Writes The Failed Tests in The Failed Cases JSON File Post Execution + * @param failedTests + */ writeFailedTests(failedTests) { fs.writeFile('failedCases.json', JSON.stringify(failedTests),function (err) { if (err) @@ -43,6 +60,11 @@ class FailedTestsRerun { }); } + /** + * Removes The Passed Test From The Failed Cases JSON During the Execution + * Deletes the File When All Tests Are Passed + * @param passedTest + */ removePassedTests(passedTest) { if (optionsForCheck.failed ) { let currentFile = JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); diff --git a/lib/workers.js b/lib/workers.js index 8a908684e..435425389 100644 --- a/lib/workers.js +++ b/lib/workers.js @@ -16,6 +16,13 @@ const recorder = require('./recorder'); const runHook = require('./hooks'); const WorkerStorage = require('./workerStorage'); +const FailedTestRerun = require('./rerunFailed'); +const failedTestRerun = new FailedTestRerun(); + +let testsArrObj = []; +let failedTests = []; +let passedTests = []; + const pathToWorker = path.join(__dirname, 'command', 'workers', 'runTests.js'); const initializeCodecept = (configPath, options = {}) => { @@ -241,6 +248,7 @@ class Workers extends EventEmitter { mocha.suite.eachTest((test) => { const i = groupCounter % groups.length; if (test) { + testsArrObj.push(test); const { id } = test; groups[i].push(id); groupCounter++; @@ -263,6 +271,7 @@ class Workers extends EventEmitter { const i = indexOfSmallestElement(groups); suite.tests.forEach((test) => { if (test) { + testsArrObj.push(test); const { id } = test; groups[i].push(id); } @@ -325,10 +334,15 @@ class Workers extends EventEmitter { break; case event.test.failed: this._updateFinishedTests(repackTest(message.data)); + const failTest = testsArrObj.filter(t => t.id === message.data.id); + failedTests.push(failTest[0].file); this.emit(event.test.failed, repackTest(message.data)); break; case event.test.passed: this._updateFinishedTests(repackTest(message.data)); + const passTest = testsArrObj.filter(t => t.id === message.data.id); + passedTests.push(passTest[0].file); + failedTestRerun.removePassedTests(passedTests); this.emit(event.test.passed, repackTest(message.data)); break; case event.test.skipped: @@ -340,6 +354,7 @@ class Workers extends EventEmitter { this.emit(event.test.after, repackTest(message.data)); break; case event.all.after: + failedTestRerun.writeFailedTests(failedTests); this._appendStats(message.data); break; } }); From 83ac71441bc3f8a393e21cfa42c2d073aa2a38a9 Mon Sep 17 00:00:00 2001 From: Manoj Rajan Date: Thu, 21 Jan 2021 13:19:56 +0530 Subject: [PATCH 3/6] Rerun of the failed scripts --- lib/codecept.js | 2 +- lib/command/run-workers.js | 2 -- lib/command/run.js | 1 - lib/listener/exit.js | 6 ++---- lib/rerunFailed.js | 34 +++++++++++++++++----------------- lib/workers.js | 2 -- 6 files changed, 20 insertions(+), 27 deletions(-) diff --git a/lib/codecept.js b/lib/codecept.js index 5606d88c8..cfef92d5a 100644 --- a/lib/codecept.js +++ b/lib/codecept.js @@ -129,7 +129,7 @@ class Codecept { cwd: global.codecept_dir, }; if(this.opts.failed){ - print('Running The Tests in Failed Re-Run Mode ..'); + print('Re-running The Failed Scripts ..'); const failedTestRerun = new FailedTestsRerun(); failedTestRerun.checkForFailedTestsExist(this.opts); this.testFiles= JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); diff --git a/lib/command/run-workers.js b/lib/command/run-workers.js index 817d6d296..07e56ccc1 100644 --- a/lib/command/run-workers.js +++ b/lib/command/run-workers.js @@ -25,8 +25,6 @@ module.exports = async function (workerCount, options) { options, }; - failedTestRerun.setOptions(options); - const numberOfWorkers = parseInt(workerCount, 10); output.print(`CodeceptJS v${require('../codecept').version()}`); diff --git a/lib/command/run.js b/lib/command/run.js index 2154dd75f..64e59f939 100644 --- a/lib/command/run.js +++ b/lib/command/run.js @@ -22,7 +22,6 @@ module.exports = async function (test, options) { createOutputDir(config, testRoot); const codecept = new Codecept(config, options); - failedTestRerun.setOptions(options); try { codecept.init(testRoot); await codecept.bootstrap(); diff --git a/lib/listener/exit.js b/lib/listener/exit.js index a9b771364..37be0133b 100644 --- a/lib/listener/exit.js +++ b/lib/listener/exit.js @@ -33,11 +33,9 @@ module.exports = function () { }); event.dispatcher.on(event.all.after, () => { - // Writes the Failed Test Names In The JSON File For Rerun - if(failedTests.length > 0) { - failedTestRerun.writeFailedTests(failedTestName); - } + failedTestRerun.writeFailedTests(failedTestName); + failedTestRerun.checkAndRemoveFailedCasesFile(failedTestName) }); process.on('beforeExit', (code) => { diff --git a/lib/rerunFailed.js b/lib/rerunFailed.js index f615dff75..2f1fcacc5 100644 --- a/lib/rerunFailed.js +++ b/lib/rerunFailed.js @@ -4,14 +4,6 @@ let optionsForCheck; class FailedTestsRerun { - /** - * Sets The Options When The Run is Initiated - * @param options - */ - setOptions(options) { - optionsForCheck = options; - } - /** * Initial Check To Find the Existence Of Failed Cases JSON File * and Content @@ -54,6 +46,7 @@ class FailedTestsRerun { * @param failedTests */ writeFailedTests(failedTests) { + if(failedTests.length > 0 ) fs.writeFile('failedCases.json', JSON.stringify(failedTests),function (err) { if (err) return print(err); @@ -66,7 +59,7 @@ class FailedTestsRerun { * @param passedTest */ removePassedTests(passedTest) { - if (optionsForCheck.failed ) { + if(fs.existsSync('failedCases.json')){ let currentFile = JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); currentFile.forEach((t) => { if (currentFile.includes(passedTest)) { @@ -76,18 +69,25 @@ class FailedTestsRerun { } } }); - if(currentFile.length === 0 ){ - fs.unlink('failedCases.json',err => { - if(err) throw err; - }); - } - else{ - fs.writeFile('failedCases.json', currentFile, function (err) { + fs.writeFile('failedCases.json', JSON.stringify(currentFile), function (err) { if (err) throw err - }); + }); + } + } + + /** + * To Remove The Failed Cases JSON file if No Tests Are There + * @param failedTests + */ + checkAndRemoveFailedCasesFile(failedTests){ + if(failedTests.length < 1) { + if (fs.existsSync('failedCases.json')) { + fs.unlinkSync('failedCases.json'); } } } + + } module.exports = FailedTestsRerun; diff --git a/lib/workers.js b/lib/workers.js index 435425389..9ec05db8e 100644 --- a/lib/workers.js +++ b/lib/workers.js @@ -63,7 +63,6 @@ const createWorker = (workerObject) => { }, }); worker.on('error', err => output.error(`Worker Error: ${err.stack}`)); - WorkerStorage.addWorker(worker); return worker; }; @@ -342,7 +341,6 @@ class Workers extends EventEmitter { this._updateFinishedTests(repackTest(message.data)); const passTest = testsArrObj.filter(t => t.id === message.data.id); passedTests.push(passTest[0].file); - failedTestRerun.removePassedTests(passedTests); this.emit(event.test.passed, repackTest(message.data)); break; case event.test.skipped: From e74fb6da7b646479affb70d136820bb9d9f19ae4 Mon Sep 17 00:00:00 2001 From: Manoj Rajan Date: Thu, 21 Jan 2021 14:37:42 +0530 Subject: [PATCH 4/6] Rerun Of the Failed scripts --- lib/rerunFailed.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rerunFailed.js b/lib/rerunFailed.js index 2f1fcacc5..1633c88f0 100644 --- a/lib/rerunFailed.js +++ b/lib/rerunFailed.js @@ -47,7 +47,8 @@ class FailedTestsRerun { */ writeFailedTests(failedTests) { if(failedTests.length > 0 ) - fs.writeFile('failedCases.json', JSON.stringify(failedTests),function (err) { + // @ts-ignore + fs.writeFileSync('failedCases.json', JSON.stringify(failedTests),function (err) { if (err) return print(err); }); From 8783ca7285641ce18835e96da44dd5ad704a7435 Mon Sep 17 00:00:00 2001 From: Manoj Rajan Date: Thu, 21 Jan 2021 18:55:29 +0530 Subject: [PATCH 5/6] Rerun Selected or Failed Tests --- bin/codecept.js | 4 ++-- lib/codecept.js | 7 +++++-- lib/rerunFailed.js | 28 +++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/bin/codecept.js b/bin/codecept.js index 5f1f18bde..d1b57f42a 100755 --- a/bin/codecept.js +++ b/bin/codecept.js @@ -95,7 +95,7 @@ program.command('run [test]') .option('--features', 'run only *.feature files and skip tests') .option('--tests', 'run only JS test files and skip features') .option('-p, --plugins ', 'enable plugins, comma-separated') - .option('--failed','to run the failed tests') + .option('--rerun-tests','to run the selected / failed tests from last execution') // mocha options .option('--colors', 'force enabling of colors') @@ -134,7 +134,7 @@ program.command('run-workers ') .option('-p, --plugins ', 'enable plugins, comma-separated') .option('-O, --reporter-options ', 'reporter-specific options') .option('-R, --reporter ', 'specify the reporter to use') - .option('--failed','to run the failed tests') + .option('--rerun-tests','to run the selected / failed tests from last execution') .action(require('../lib/command/run-workers')); program.command('run-multiple [suites...]') diff --git a/lib/codecept.js b/lib/codecept.js index cfef92d5a..e415e4d25 100644 --- a/lib/codecept.js +++ b/lib/codecept.js @@ -125,14 +125,17 @@ class Codecept { * @param {string} [pattern] */ loadTests(pattern){ + let flag = 0; + let invalidTest = []; const options = { cwd: global.codecept_dir, }; - if(this.opts.failed){ - print('Re-running The Failed Scripts ..'); + if(this.opts.rerunTests){ + print('Re-running The Selected/Failed Scripts ..'); const failedTestRerun = new FailedTestsRerun(); failedTestRerun.checkForFailedTestsExist(this.opts); this.testFiles= JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); + failedTestRerun.checkForFileExistence(this.testFiles); } else { let patterns = [pattern]; diff --git a/lib/rerunFailed.js b/lib/rerunFailed.js index 1633c88f0..5b9ace526 100644 --- a/lib/rerunFailed.js +++ b/lib/rerunFailed.js @@ -1,6 +1,5 @@ const fs = require('fs'); const { print } = require('codeceptjs/lib/output'); -let optionsForCheck; class FailedTestsRerun { @@ -10,7 +9,7 @@ class FailedTestsRerun { * @param options */ checkForFailedTestsExist(options) { - if (options.failed) { + if (options.rerunTests) { if (!fs.existsSync('failedCases.json')) { print('There Are No Failed Tests In Previous Execution'); process.exit() @@ -25,6 +24,29 @@ class FailedTestsRerun { } } + /** + * Checks If The Valid Files are Passed in Failed Cases Json File + *@param testFiles + */ + checkForFileExistence(testFiles){ + let invalidTest = []; + let flag = 0; + for(let i = 0;i < testFiles.length;i++) { + let path = testFiles[i]; + if(!fs.existsSync(path)){ + invalidTest.push(path); + flag++; + } + } + if(flag > 0){ + print("\nInvalid File(s) Found :" + '\n' ); + for(let j = 0; j< invalidTest.length;j++){ + print(invalidTest[j]); + } + process.exit(); + } + } + /** * Returns The Content In JSON File For File/Content existence Check * @return {string[]} @@ -34,7 +56,7 @@ class FailedTestsRerun { } /** - * Returns The Failed Tests + * Returns The Failed/Selected Tests * @return {any} */ getFailedTests() { From 0fa25aa23eed519aaa205df839a2093fbe0858b7 Mon Sep 17 00:00:00 2001 From: Manoj Rajan Date: Thu, 21 Jan 2021 20:34:23 +0530 Subject: [PATCH 6/6] Fixed eslint issues --- lib/command/run.js | 4 ++-- lib/rerunFailed.js | 54 +++++++++++++++++++++------------------------- lib/workers.js | 9 +++++--- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/lib/command/run.js b/lib/command/run.js index 64e59f939..88a8ecaa5 100644 --- a/lib/command/run.js +++ b/lib/command/run.js @@ -3,9 +3,9 @@ const { } = require('./utils'); const Config = require('../config'); const Codecept = require('../codecept'); -const FailedTestRerun = require('./../rerunFailed'); +const FailedTestRerun = require('../rerunFailed'); + const failedTestRerun = new FailedTestRerun(); -const {stringify} = require('flatted') module.exports = async function (test, options) { // registering options globally to use in config diff --git a/lib/rerunFailed.js b/lib/rerunFailed.js index 5b9ace526..e6143966a 100644 --- a/lib/rerunFailed.js +++ b/lib/rerunFailed.js @@ -2,7 +2,6 @@ const fs = require('fs'); const { print } = require('codeceptjs/lib/output'); class FailedTestsRerun { - /** * Initial Check To Find the Existence Of Failed Cases JSON File * and Content @@ -12,9 +11,8 @@ class FailedTestsRerun { if (options.rerunTests) { if (!fs.existsSync('failedCases.json')) { print('There Are No Failed Tests In Previous Execution'); - process.exit() - } - else{ + process.exit(); + } else { const failedTests = this.getFailedTestContent(); if (failedTests.length === 0 || failedTests.toString() === '') { print('There Are No Files Present In the Failed Cases Json File'); @@ -28,19 +26,20 @@ class FailedTestsRerun { * Checks If The Valid Files are Passed in Failed Cases Json File *@param testFiles */ - checkForFileExistence(testFiles){ - let invalidTest = []; + checkForFileExistence(testFiles) { + const invalidTest = []; let flag = 0; - for(let i = 0;i < testFiles.length;i++) { - let path = testFiles[i]; - if(!fs.existsSync(path)){ + for (let i = 0; i < testFiles.length; i++) { + const path = testFiles[i]; + if (!fs.existsSync(path)) { invalidTest.push(path); flag++; } } - if(flag > 0){ - print("\nInvalid File(s) Found :" + '\n' ); - for(let j = 0; j< invalidTest.length;j++){ + if (flag > 0) { + // eslint-disable-next-line no-useless-concat + print('\nInvalid File(s) Found :' + '\n'); + for (let j = 0; j < invalidTest.length; j++) { print(invalidTest[j]); } process.exit(); @@ -51,8 +50,8 @@ class FailedTestsRerun { * Returns The Content In JSON File For File/Content existence Check * @return {string[]} */ - getFailedTestContent(){ - return fs.readFileSync('failedCases.json', {encoding: 'utf8'}).split(','); + getFailedTestContent() { + return fs.readFileSync('failedCases.json', { encoding: 'utf8' }).split(','); } /** @@ -61,19 +60,18 @@ class FailedTestsRerun { */ getFailedTests() { return JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); - } + } /** * Writes The Failed Tests in The Failed Cases JSON File Post Execution * @param failedTests */ writeFailedTests(failedTests) { - if(failedTests.length > 0 ) - // @ts-ignore - fs.writeFileSync('failedCases.json', JSON.stringify(failedTests),function (err) { - if (err) - return print(err); - }); + if (failedTests.length > 0) { + fs.writeFileSync('failedCases.json', JSON.stringify(failedTests), (err) => { + if (err) { return print(err); } + }); + } } /** @@ -82,8 +80,8 @@ class FailedTestsRerun { * @param passedTest */ removePassedTests(passedTest) { - if(fs.existsSync('failedCases.json')){ - let currentFile = JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); + if (fs.existsSync('failedCases.json')) { + const currentFile = JSON.parse(fs.readFileSync('failedCases.json', 'utf8')); currentFile.forEach((t) => { if (currentFile.includes(passedTest)) { const index = currentFile.indexOf(t); @@ -92,8 +90,8 @@ class FailedTestsRerun { } } }); - fs.writeFile('failedCases.json', JSON.stringify(currentFile), function (err) { - if (err) throw err + fs.writeFile('failedCases.json', JSON.stringify(currentFile), (err) => { + if (err) throw err; }); } } @@ -102,15 +100,13 @@ class FailedTestsRerun { * To Remove The Failed Cases JSON file if No Tests Are There * @param failedTests */ - checkAndRemoveFailedCasesFile(failedTests){ - if(failedTests.length < 1) { + checkAndRemoveFailedCasesFile(failedTests) { + if (failedTests.length < 1) { if (fs.existsSync('failedCases.json')) { fs.unlinkSync('failedCases.json'); } } } - - } module.exports = FailedTestsRerun; diff --git a/lib/workers.js b/lib/workers.js index 9ec05db8e..43616c992 100644 --- a/lib/workers.js +++ b/lib/workers.js @@ -17,11 +17,12 @@ const runHook = require('./hooks'); const WorkerStorage = require('./workerStorage'); const FailedTestRerun = require('./rerunFailed'); + const failedTestRerun = new FailedTestRerun(); -let testsArrObj = []; -let failedTests = []; -let passedTests = []; +const testsArrObj = []; +const failedTests = []; +const passedTests = []; const pathToWorker = path.join(__dirname, 'command', 'workers', 'runTests.js'); @@ -333,12 +334,14 @@ class Workers extends EventEmitter { break; case event.test.failed: this._updateFinishedTests(repackTest(message.data)); + // eslint-disable-next-line no-case-declarations const failTest = testsArrObj.filter(t => t.id === message.data.id); failedTests.push(failTest[0].file); this.emit(event.test.failed, repackTest(message.data)); break; case event.test.passed: this._updateFinishedTests(repackTest(message.data)); + // eslint-disable-next-line no-case-declarations const passTest = testsArrObj.filter(t => t.id === message.data.id); passedTests.push(passTest[0].file); this.emit(event.test.passed, repackTest(message.data));