-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added json reporter for rerun * Added rerun functionality for failed tests --------- Co-authored-by: Harshit Agrawal <harshit.a@browserstack.com> Co-authored-by: Harshit Agrawal <94462364+harshit-bs@users.noreply.github.com> Co-authored-by: Binayak Ghosh <ghoshbinayak@gmail.com>
- Loading branch information
1 parent
7f72ce2
commit 020601e
Showing
9 changed files
with
345 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const lodashPick = require('lodash.pick'); | ||
const path = require('path'); | ||
const Utils = require('../../utils'); | ||
const {Logger} = Utils; | ||
const BaseReporter = require('../base-reporter.js'); | ||
|
||
class MinimalJsonReporter extends BaseReporter { | ||
|
||
adaptModule(module) { | ||
// Pick only the necessary fields | ||
const result = lodashPick(module, [ | ||
'modulePath', | ||
'status' | ||
]); | ||
|
||
return result; | ||
} | ||
|
||
adaptResults() { | ||
const {modules} = this.results; | ||
this.modules = {}; | ||
|
||
Object.keys(modules).forEach((moduleKey) => { | ||
this.modules[moduleKey] = this.adaptModule(modules[moduleKey]); | ||
}); | ||
|
||
return this.modules; | ||
} | ||
|
||
writeReport() { | ||
const results = this.adaptResults(); | ||
const {output_folder} = this.options; | ||
const filename = path.join(output_folder, 'minimal_report.json'); | ||
const shouldCreateFolder = !Utils.dirExistsSync(output_folder); | ||
|
||
const report = { | ||
modules: results | ||
}; | ||
|
||
return this.writeReportFile(filename, JSON.stringify(report), shouldCreateFolder, output_folder) | ||
.then(_ => { | ||
Logger.info(Logger.colors.stack_trace(`Wrote Rerun Json report file to: ${path.resolve(filename)}`)); | ||
|
||
// Setting env varaible with minimalJsonReporter path. | ||
// Next time user runs nightwatch with rerun functionality, json reporter can be read from this | ||
process.env.NIGHTWATCH_RERUN_REPORT_FILE = filename; | ||
process.env.NEW_FOO = filename; | ||
}); | ||
} | ||
} | ||
|
||
function write(results, options, callback) { | ||
const reporter = new MinimalJsonReporter(results, options); | ||
|
||
reporter.writeReport() | ||
.then(_ => { | ||
callback(); | ||
}) | ||
.catch(err => { | ||
Logger.error(err); | ||
callback(err); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
MinimalJsonReporter, | ||
write | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"modules": { | ||
"demoTagTest": { | ||
"modulePath": "./test/sampletests/tags/sample.js", | ||
"status": "pass" | ||
}, | ||
"demoTest": { | ||
"modulePath": "./test/sampletests/simple/test/sample.js", | ||
"status": "fail" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const path = require('path'); | ||
const assert = require('assert'); | ||
const common = require('../../common.js'); | ||
const CommandGlobals = require('../../lib/globals/commands.js'); | ||
const MockServer = require('../../lib/mockserver.js'); | ||
const {settings} = common; | ||
const {runTests} = common.require('index.js'); | ||
|
||
describe('testRerun', function () { | ||
beforeEach(function (done) { | ||
process.removeAllListeners('exit'); | ||
process.removeAllListeners('uncaughtException'); | ||
process.removeAllListeners('unhandledRejection'); | ||
|
||
this.server = MockServer.init(); | ||
|
||
this.server.on('listening', () => { | ||
done(); | ||
}); | ||
}); | ||
|
||
afterEach(function (done) { | ||
CommandGlobals.afterEach.call(this, function () { | ||
Object.keys(require.cache).forEach(function (module) { | ||
delete require.cache[module]; | ||
}); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('Rerun with --rerun-failed cli argument without setting env variable for json reporter', function () { | ||
|
||
return runTests({ | ||
'rerun-failed': true | ||
}, settings({ | ||
output: false | ||
})).catch(err => { | ||
return err; | ||
}).then(err => { | ||
assert.ok(err instanceof Error); | ||
assert.strictEqual(err.message, 'Unable to find the Json reporter file to rerun failed tests'); | ||
assert.strictEqual(err.detailedErr, 'Configure the environment variable NIGHTWATCH_RERUN_REPORT_FILE with Json reporter file path'); | ||
}); | ||
}); | ||
|
||
it('Rerun with env varaible without setting env variable for json reporter', function () { | ||
process.env.NIGHTWATCH_RERUN_FAILED = 'true'; | ||
|
||
return runTests({ | ||
'rerun-failed': true | ||
}, settings({ | ||
output: false | ||
})).catch(err => { | ||
return err; | ||
}).then(err => { | ||
assert.ok(err instanceof Error); | ||
assert.strictEqual(err.message, 'Unable to find the Json reporter file to rerun failed tests'); | ||
assert.strictEqual(err.detailedErr, 'Configure the environment variable NIGHTWATCH_RERUN_REPORT_FILE with Json reporter file path'); | ||
|
||
delete process.env.NIGHTWATCH_RERUN_FAILED; | ||
}); | ||
}); | ||
|
||
it('Rerun with env varaible set and with setting env variable for json reporter', function () { | ||
process.env.NIGHTWATCH_RERUN_FAILED = 'true'; | ||
process.env.NIGHTWATCH_RERUN_REPORT_FILE = path.join(__dirname, '../../extra/minimalJsonReporter.json'); | ||
|
||
return runTests({ | ||
}, settings({ | ||
output: false, | ||
globals: { | ||
reporter(results) { | ||
assert.strictEqual(Object.keys(results.modules).length, 1); | ||
assert.strictEqual(results.assertions, 2); | ||
} | ||
} | ||
})).then(err => { | ||
assert.strictEqual(err, false); | ||
|
||
delete process.env.NIGHTWATCH_RERUN_FAILED; | ||
delete process.env.NIGHTWATCH_RERUN_REPORT_FILE; | ||
}); | ||
}); | ||
|
||
it('Rerun with cli flag and with setting env variable for json reporter', function () { | ||
process.env.NIGHTWATCH_RERUN_REPORT_FILE = path.join(__dirname, '../../extra/minimalJsonReporter.json'); | ||
|
||
return runTests({ | ||
'rerun-failed': true | ||
}, settings({ | ||
output: false, | ||
globals: { | ||
reporter(results) { | ||
assert.strictEqual(Object.keys(results.modules).length, 1); | ||
assert.strictEqual(results.assertions, 2); | ||
} | ||
} | ||
})).then(err => { | ||
assert.strictEqual(err, false); | ||
delete process.env.NIGHTWATCH_RERUN_REPORT_FILE; | ||
}); | ||
}); | ||
|
||
|
||
}); |
Oops, something went wrong.