Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bin/codecept.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pattern>', 'select only tests matching <pattern>')
.option('-f, --fgrep <string>', 'select only tests containing <string>')
.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();
Expand Down
14 changes: 14 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
89 changes: 89 additions & 0 deletions lib/command/listOfTests.js
Original file line number Diff line number Diff line change
@@ -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);
}