diff --git a/bin/codecept.js b/bin/codecept.js index b1e0a3737..29f9dd40a 100755 --- a/bin/codecept.js +++ b/bin/codecept.js @@ -180,6 +180,14 @@ program.command('dry-run [test]') .option('--debug', 'output additional information') .action(require('../lib/command/dryRun')); +program.command('list-of-tests') + .description('Prints a list of tests in json format') + .option('-g, --grep ', 'select only tests matching ') + .option('-f, --fgrep ', 'select only tests containing ') + .option('-i, --invert', 'invert --grep and --fgrep matches') + .option('--file', 'save information to output directory') + .action(require('../lib/command/listOfTests')); + program.on('command:*', (cmd) => { console.log(`\nUnknown command ${cmd}\n`); program.outputHelp(); diff --git a/docs/commands.md b/docs/commands.md index c1132dd36..3b54a6a3e 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -236,3 +236,17 @@ Prints debugging information concerning the local environment ```sh npx codeceptjs info ``` + +## List of tests + +Prints a list of tests in json format + +``` +npx codeceptjs list-of-tests +``` + +When passed `--file` option saves a list of tests to output directory. + +``` +npx codeceptjs list-of-tests --file +``` diff --git a/lib/command/listOfTests.js b/lib/command/listOfTests.js new file mode 100644 index 000000000..c233fc3e0 --- /dev/null +++ b/lib/command/listOfTests.js @@ -0,0 +1,89 @@ +const { getConfig, getTestRoot } = require('./utils'); +const Codecept = require('../codecept'); +const output = require('../output'); +const Container = require('../container'); + +module.exports = async function (options) { + const config = getConfig(); + const testRoot = getTestRoot(); + const output = config.output || '.'; + + try { + const codecept = new Codecept(config, options); + codecept.init(testRoot); + codecept.loadTests(); + + const listOfTests = getListOfTests(codecept.testFiles); + + if (!options.file) { + printTests(listOfTests); + } else { + saveTests(listOfTests, output); + } + } catch (err) { + console.error(err); + process.exit(1); + } +}; + +function getListOfTests(files) { + const mocha = Container.mocha(); + mocha.files = files; + mocha.loadFiles(); + + const grep = mocha.options.grep || /.*/; + const invert = !!mocha.options.invert; + + const listOfTests = { + suites: [], + }; + + for (const suite of mocha.suite.suites) { + const suiteNode = { + title: suite.title, + path: suite.file || '', + tests: [], + }; + + for (const test of suite.tests) { + let match = grep.test(test.fullTitle()); + + if (invert) { + match = !match; + } + + if (match) { + const testNode = { + title: test.title, + fullTitle: test.fullTitle(), + skipped: !!test.isPending(), + }; + + suiteNode.tests.push(testNode); + } + } + + if (suiteNode.tests.length) { + listOfTests.suites.push(suiteNode); + } + } + + return JSON.stringify(listOfTests, null, '\t'); +} + +function printTests(listOfTests) { + output.print(output.styles.debug(`Tests from ${global.codecept_dir}:`)); + output.print(); + + output.print(listOfTests); +} + +function saveTests(listOfTests, path) { + const fs = require('fs'); + + if (!fs.existsSync(path)) { + fs.mkdirSync(path, { recursive: true }); + } + + fs.writeFileSync(`${path.replace(/\$/, '')}/list_of_tests.json`, listOfTests); +}