Skip to content

Commit

Permalink
Update logging output to use JSON for the list-files cli argument (#3619
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Vaibhav Singh authored Feb 28, 2023
1 parent 6e6e715 commit 858e4af
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
18 changes: 10 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ Nightwatch.cli = function(callback) {
const ArgvSetup = require('./runner/cli/argv-setup.js');
const {argv} = ArgvSetup;

if (argv['list-files']) {
argv._source = argv['_'].slice(0);
const runner = Nightwatch.CliRunner(argv);

return runner.setupAsync()
.then(() => runner.getTestsFiles())
// eslint-disable-next-line no-console
.then((result) => console.log(JSON.stringify(result)));
}

if (argv.help) {
ArgvSetup.showHelp();
} else if (argv.info) {
Expand All @@ -201,14 +211,6 @@ Nightwatch.cli = function(callback) {
).then(console.log);
} else if (argv.version) {
Utils.printVersionInfo();
} else if (argv['list-files']) {
argv._source = argv['_'].slice(0);
const runner = Nightwatch.CliRunner(argv);

runner.setupAsync()
.then(() => runner.getTestsFiles())
// eslint-disable-next-line no-console
.then((result) => console.log(Logger.inspectObject(result)));
} else {
if (!Utils.isFunction(callback)) {
throw new Error('Supplied callback argument needs to be a function.');
Expand Down
4 changes: 2 additions & 2 deletions lib/reporter/global-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ module.exports = class GlobalReporter {
this.reporterFile = reporter;
this.reportFileName = reportFileName;

if (!Array.isArray(this.reporterFile)) {
this.reporterFile = [reporter];
if (!Array.isArray(this.reporterFile) && typeof this.reporterFile == 'string') {
this.reporterFile = this.reporterFile.split(',');
}
this.settings = settings;
this.summary = new Summary(settings);
Expand Down
57 changes: 56 additions & 1 deletion test/src/cli/testCliRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ describe('Test CLI Runner', function() {

mockery.registerMock('fs', {
existsSync() {
return false
return false;
},
stat(file, cb) {
if (file === TEST_SRC_PATH || file === './custom.js') {
Expand Down Expand Up @@ -1288,4 +1288,59 @@ describe('Test CLI Runner', function() {
assert.strictEqual(runner.test_settings.desiredCapabilities['moz:firefoxOptions'].androidPackage, 'org.mozilla.firefox');
assert.strictEqual(runner.test_settings.desiredCapabilities['moz:firefoxOptions'].androidDeviceSerial, 'ZD2222W62Y');
});

describe('Test \'list-files\' flag', () => {
it('output list of files - default environment', async () => {
const testsPath = [origPath.join(__dirname, origPath.join('..', '..', 'sampletests', 'simple', 'test', 'sample.js'))];
const consoleData = [];
const listFileOutput = JSON.stringify({
default: testsPath
});

const origConsoleLog = console.log;

console.log = function (data) {
consoleData.push(data);
};

mockery.registerMock('./runner/cli/argv-setup.js', {
argv: {
_: testsPath,
'list-files': true
}
});
const NwClient = common.require('index.js');
await NwClient.cli();
assert.deepStrictEqual(listFileOutput, consoleData[0]);
console.log = origConsoleLog;
});

it('output list of files - chrome environment', async () => {
const testsPath = [origPath.join(__dirname, origPath.join('..', '..', 'sampletests', 'simple', 'test', 'sample.js'))];
const consoleData = [];
const listFileOutput = JSON.stringify({
chrome: testsPath
});

const origConsoleLog = console.log;

console.log = function (data) {
consoleData.push(data);
};

mockery.registerMock('./runner/cli/argv-setup.js', {
argv: {
_: testsPath,
env: 'chrome',
'list-files': true
}
});
const NwClient = common.require('index.js');
await NwClient.cli();
assert.deepStrictEqual(listFileOutput, consoleData[0]);
console.log = origConsoleLog;
});
});


});
28 changes: 28 additions & 0 deletions test/src/runner/testReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,32 @@ describe('testReporter', function() {
}
assert.strictEqual(possibleError, null);
});

it('test with multiple reporters', function() {
mockery.registerMock('nightwatch_reporter', {
async write(_results, _options) {

return 'nightwatch_reporter_output';
}
});
mockery.registerMock('html_reporter', {
async write(_results, _options) {

return 'html_reporter_output';
}
});

const reporter = new Reporter('nightwatch_reporter,html_reporter', {
globals: {
reporter(_results, done) {
done();
}
},
output_folder: 'output'
});

return reporter.writeReportToFile().then(function(result) {
assert.deepStrictEqual(result, ['nightwatch_reporter_output', 'html_reporter_output']);
});
});
});

0 comments on commit 858e4af

Please sign in to comment.