Welcome to mirror list, hosted at ThFree Co, Russian Federation.

helpers.js « integration « test « mocha-3.1.2 « lib « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f6a608a6f24877775aa8c48cecad7040896fe5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';

var spawn = require('child_process').spawn;
var path = require('path');
var fs = require('fs');
var baseReporter = require('../../lib/reporters/base');

module.exports = {
  /**
   * Invokes the mocha binary for the given fixture with color output disabled.
   * Accepts an array of additional command line args to pass. The callback is
   * invoked with a summary of the run, in addition to its output. The summary
   * includes the number of passing, pending, and failing tests, as well as the
   * exit code. Useful for testing different reporters.
   *
   * Example response:
   * {
   *   pending: 0,
   *   passing: 0,
   *   failing: 1,
   *   code:    1,
   *   output:  '...'
   * }
   *
   * @param {string}   fixturePath
   * @param {array}    args
   * @param {function} fn
   */
  runMocha: function (fixturePath, args, fn) {
    var path;

    path = resolveFixturePath(fixturePath);
    args = args || [];

    invokeMocha(args.concat(['-C', path]), function (err, res) {
      if (err) return fn(err);

      fn(null, getSummary(res));
    });
  },

  /**
   * Invokes the mocha binary on the code of the body of the function.
   * Accepts an array of additional command line args to pass. The callback is
   * invoked with a summary of the run, in addition to its output. The summary
   * includes the number of passing, pending, and failing tests, as well as the
   * exit code. Useful for testing different reporters.
   *
   * Example response:
   * {
   *   pending: 0,
   *   passing: 0,
   *   failing: 1,
   *   code:    1,
   *   output:  '...'
   * }
   *
   * @param {function} fixture
   * @param {array}    args
   * @param {function} fn
   */
  runMochaFunction: function (fixture, args, fn) {
    var path = resolveFixturePath(fixture.name + '.js' || 'tempfile.js');
    args = args || [];

    var fixtureContent = 'var fn = ' + fixture.toString() + '; fn()';
    fs.writeFileSync(path, fixtureContent, 'utf8');

    function cleanup () {
      fs.unlink(path);
      fn.apply(this, arguments);
    }

    invokeMocha(args.concat(['-C', path]), function (err, res) {
      if (err) {
        return cleanup(err);
      }

      cleanup(null, getSummary(res));
    });
  },

  /**
   * Invokes the mocha binary for the given fixture using the JSON reporter,
   * returning the parsed output, as well as exit code.
   *
   * @param {string}   fixturePath
   * @param {array}    args
   * @param {function} fn
   */
  runMochaJSON: function (fixturePath, args, fn) {
    var path;

    path = resolveFixturePath(fixturePath);
    args = args || [];

    invokeMocha(args.concat(['--reporter', 'json', path]), function (err, res) {
      if (err) return fn(err);

      try {
        var result = JSON.parse(res.output);
        result.code = res.code;
      } catch (err) {
        return fn(err);
      }

      fn(null, result);
    });
  },

  /**
   * Returns an array of diffs corresponding to exceptions thrown from specs,
   * given the plaintext output (-C) of a mocha run.
   *
   * @param  {string}   output
   * returns {string[]}
   */
  getDiffs: function (output) {
    var diffs, i, inDiff, inStackTrace;

    diffs = [];
    output.split('\n').forEach(function (line) {
      if (line.match(/^\s{2}\d+\)/)) {
        // New spec, e.g. "1) spec title"
        diffs.push([]);
        i = diffs.length - 1;
        inStackTrace = false;
        inDiff = false;
      } else if (!diffs.length || inStackTrace) {
        // Haven't encountered a spec yet
        // or we're in the middle of a stack trace
        return;
      } else if (line.indexOf('+ expected - actual') !== -1) {
        inDiff = true;
      } else if (line.match(/at Context/)) {
        // At the start of a stack trace
        inStackTrace = true;
        inDiff = false;
      } else if (inDiff) {
        diffs[i].push(line);
      }
    });

    // Ignore empty lines before/after diff
    return diffs.map(function (diff) {
      return diff.slice(1, -3).join('\n');
    });
  },

  /**
   * regular expression used for splitting lines based on new line / dot symbol.
   */
  splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+')
};

function invokeMocha (args, fn) {
  var output, mocha, listener;

  output = '';
  args = [path.join('bin', 'mocha')].concat(args);
  mocha = spawn(process.execPath, args);

  listener = function (data) {
    output += data;
  };

  mocha.stdout.on('data', listener);
  mocha.stderr.on('data', listener);
  mocha.on('error', fn);

  mocha.on('close', function (code) {
    fn(null, {
      output: output.split('\n').join('\n'),
      code: code
    });
  });
}

function resolveFixturePath (fixture) {
  return path.join('./test/integration/fixtures', fixture);
}

function getSummary (res) {
  return ['passing', 'pending', 'failing'].reduce(function (summary, type) {
    var pattern, match;

    pattern = new RegExp('  (\\d+) ' + type + '\\s');
    match = pattern.exec(res.output);
    summary[type] = (match) ? parseInt(match, 10) : 0;

    return summary;
  }, res);
}