UNPKG

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