UNPKG

1.12 kBJavaScriptView Raw
1var fs = require('fs');
2
3/**
4 * Recursive function, takes a mocha test suite and returns a flattened list of
5 * test found within
6 */
7function getTests(suite) {
8 var tests = [];
9
10 suite.tests.forEach(function(t) {
11 tests.push({
12 file: t.file,
13 title: t.title,
14 fullTitle: t.fullTitle(),
15 pending: t.pending
16 });
17 });
18
19 suite.suites.forEach(function(s) {
20 tests = tests.concat(getTests(s));
21 });
22
23 return tests;
24}
25
26/**
27 * Used as a mocha repoter for the test capturing phase
28 */
29module.exports = function(runner, options) {
30 var outputPath = process.env.MOCHA_CAPTURE_PATH;
31 if (!outputPath) {
32 throw new Error('Environment variable MOCHA_CAPTURE_PATH must be defined');
33 }
34
35 // capture but do not run tests
36 runner.run = function(done) {
37 done();
38 }
39
40 // traverse suite structure and flattened list of tests
41 var tests = getTests(runner.suite);
42
43 // process .only greps
44 if (options.grep) {
45 tests = tests.filter(function(t) {
46 return t.fullTitle.match(options.grep);
47 });
48 }
49
50 fs.writeFileSync(process.env.MOCHA_CAPTURE_PATH, JSON.stringify(tests));
51};