How to use invokeMocha method in Mocha

Best JavaScript code snippet using mocha

Run Mocha automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

helpers.js

Source: helpers.js Github

copy
1'use strict';
2
3var spawn = require('cross-spawn').spawn;
4var path = require('path');
5var baseReporter = require('../../lib/reporters/base');
6
7module.exports = {
8  /**
9   * Invokes the mocha binary for the given fixture with color output disabled.
10   * Accepts an array of additional command line args to pass. The callback is
11   * invoked with a summary of the run, in addition to its output. The summary
12   * includes the number of passing, pending, and failing tests, as well as the
13   * exit code. Useful for testing different reporters.
14   *
15   * Example response:
16   * {
17   *   pending: 0,
18   *   passing: 0,
19   *   failing: 1,
20   *   code:    1,
21   *   output:  '...'
22   * }
23   *
24   * @param {string} fixturePath - Path to fixture .js file
25   * @param {Array<string>} args - Extra args to mocha executable
26   * @param {Function} done - Callback
27   */
28  runMocha: function(fixturePath, args, done) {
29    var path;
30
31    path = resolveFixturePath(fixturePath);
32    args = args || [];
33
34    invokeMocha(args.concat(['-C', path]), function(err, res) {
35      if (err) {
36        return done(err);
37      }
38
39      done(null, getSummary(res));
40    });
41  },
42
43  /**
44   * Invokes the mocha binary for the given fixture using the JSON reporter,
45   * returning the parsed output, as well as exit code.
46   *
47   * @param {string} fixturePath - Path from __dirname__
48   * @param {string[]} args - Array of args
49   * @param {Function} fn - Callback
50   */
51  runMochaJSON: function(fixturePath, args, fn) {
52    var path;
53
54    path = resolveFixturePath(fixturePath);
55    args = args || [];
56
57    return invokeMocha(args.concat(['--reporter', 'json', path]), function(
58      err,
59      res
60    ) {
61      if (err) return fn(err);
62
63      try {
64        var result = JSON.parse(res.output);
65        result.code = res.code;
66      } catch (err) {
67        return fn(err);
68      }
69
70      fn(null, result);
71    });
72  },
73
74  /**
75   * regular expression used for splitting lines based on new line / dot symbol.
76   */
77  splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+'),
78
79  /**
80   * Invokes the mocha binary. Accepts an array of additional command line args
81   * to pass. The callback is invoked with the exit code and output. Optional
82   * current working directory as final parameter.
83   *
84   * In most cases runMocha should be used instead.
85   *
86   * Example response:
87   * {
88   *   code:    1,
89   *   output:  '...'
90   * }
91   *
92   * @param {Array<string>} args - Extra args to mocha executable
93   * @param {Function} done - Callback
94   * @param {string} cwd - Current working directory for mocha run, optional
95   */
96  invokeMocha: invokeMocha,
97
98  /**
99   * Resolves the path to a fixture to the full path.
100   */
101  resolveFixturePath: resolveFixturePath
102};
103
104function invokeMocha(args, fn, cwd) {
105  var output, mocha, listener;
106
107  output = '';
108  args = [path.join(__dirname, '..', '..', 'bin', 'mocha')].concat(args);
109  mocha = spawn(process.execPath, args, {cwd: cwd});
110
111  listener = function(data) {
112    output += data;
113  };
114
115  mocha.stdout.on('data', listener);
116  mocha.stderr.on('data', listener);
117  mocha.on('error', fn);
118
119  mocha.on('close', function(code) {
120    fn(null, {
121      output: output.split('\n').join('\n'),
122      code: code
123    });
124  });
125
126  return mocha;
127}
128
129function resolveFixturePath(fixture) {
130  return path.join('./test/integration/fixtures', fixture);
131}
132
133function getSummary(res) {
134  return ['passing', 'pending', 'failing'].reduce(function(summary, type) {
135    var pattern, match;
136
137    pattern = new RegExp('  (\\d+) ' + type + '\\s');
138    match = pattern.exec(res.output);
139    summary[type] = match ? parseInt(match, 10) : 0;
140
141    return summary;
142  }, res);
143}
144
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Mocha on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)