mocha-distributed 0.9.4 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +45 -21
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -66,18 +66,18 @@ function captureStream(stream) {
66
66
  var oldWrite = stream.write;
67
67
  var buf = [];
68
68
 
69
- stream.write = function(chunk, encoding, callback){
70
- buf.push (chunk.toString()); // chunk is a String or Buffer
69
+ stream.write = function (chunk, encoding, callback) {
70
+ buf.push(chunk.toString()); // chunk is a String or Buffer
71
71
  oldWrite.apply(stream, arguments);
72
- }
72
+ };
73
73
 
74
74
  return {
75
- unhook(){
76
- stream.write = oldWrite;
75
+ unhook() {
76
+ stream.write = oldWrite;
77
77
  },
78
- captured(){
78
+ captured() {
79
79
  return buf;
80
- }
80
+ },
81
81
  };
82
82
  }
83
83
 
@@ -101,7 +101,7 @@ exports.mochaGlobalSetup = async function () {
101
101
  }
102
102
 
103
103
  if (!g_redisAddress || !g_testExecutionId) {
104
- console.log (g_redisAddress, g_testExecutionId)
104
+ console.log(g_redisAddress, g_testExecutionId);
105
105
  console.error(
106
106
  "You need to set at least the following environment variables:\n" +
107
107
  " - MOCHA_DISTRIBUTED\n" +
@@ -154,29 +154,32 @@ exports.mochaHooks = {
154
154
  if (assignedRunnerId !== g_runnerId) {
155
155
  this.currentTest.title += " (skipped by mocha_distributted)";
156
156
  this.skip();
157
- }
158
- else {
157
+ } else {
159
158
  g_capture.stdout = captureStream(process.stdout);
160
159
  g_capture.stderr = captureStream(process.stderr);
161
160
  }
162
161
  },
162
+
163
163
  afterEach(done) {
164
164
  const SKIPPED = "pending";
165
165
  const FAILED = "failed";
166
166
  const PASSED = "passed";
167
167
 
168
- let capturedStdout = '';
169
- let capturedStderr = '';
168
+ let capturedStdout = "";
169
+ let capturedStderr = "";
170
170
  if (g_capture.stdout) {
171
171
  const stdoutArray = g_capture.stdout.captured();
172
- capturedStdout = stdoutArray.join('');
173
- capturedStdout = capturedStdout.replace(/\s*\u001b\[3[12]m[^\n]*\n$/g, '');
172
+ capturedStdout = stdoutArray.join("");
173
+ capturedStdout = capturedStdout.replace(
174
+ /\s*\u001b\[3[12]m[^\n]*\n$/g,
175
+ ""
176
+ );
174
177
  g_capture.stdout.unhook();
175
178
  g_capture.stdout = null;
176
179
  }
177
180
 
178
181
  if (g_capture.stderr) {
179
- capturedStderr = g_capture.stderr.captured().join('');
182
+ capturedStderr = g_capture.stderr.captured().join("");
180
183
  g_capture.stderr.unhook();
181
184
  g_capture.stderr = null;
182
185
  }
@@ -184,7 +187,28 @@ exports.mochaHooks = {
184
187
  // Save all data in redis in a way it can be retrieved and aggregated
185
188
  // easily for all test by an external reporter
186
189
  if (this.currentTest.state !== SKIPPED) {
187
- const stateFixed = this.currentTest.state || (this.currentTest.timedOut ? FAILED : PASSED)
190
+ const retryAttempt = this.currentTest._currentRetry || 0;
191
+ const retryTotal = this.currentTest._retries || 1;
192
+
193
+ // adjust state value accounting for exceptions, timeouts & retries
194
+ let stateFixed = PASSED;
195
+ if (
196
+ this.currentTest.state === FAILED ||
197
+ this.currentTest.timedOut ||
198
+ (typeof this.currentTest.state === "undefined" &&
199
+ retryAttempt < retryTotal)
200
+ ) {
201
+ stateFixed = FAILED;
202
+ }
203
+
204
+ // Error objects cannot be properly serialized with stringify, thus
205
+ // we need to use this hack to make it look like a normal object.
206
+ // Hopefully this should work as well with other sort of objects
207
+ const err = this.currentTest.err || null;
208
+ const errObj = JSON.parse(
209
+ JSON.stringify(err, Object.getOwnPropertyNames(err || {}))
210
+ );
211
+
188
212
  const testResult = {
189
213
  id: getTestPath(this.currentTest),
190
214
  type: this.currentTest.type,
@@ -193,15 +217,15 @@ exports.mochaHooks = {
193
217
  duration: this.currentTest.duration,
194
218
  startTime: Date.now() - (this.currentTest.duration || 0),
195
219
  endTime: Date.now(),
196
- retryAttempt: this.currentTest._currentRetry || 0,
197
- retryTotal: this.currentTest._retries || 1,
220
+ retryAttempt: retryAttempt,
221
+ retryTotal: retryTotal,
198
222
  file: this.currentTest.file,
199
223
  state: stateFixed,
200
224
  failed: stateFixed === FAILED,
201
225
  speed: this.currentTest.speed,
202
- err: this.currentTest.err || null,
226
+ err: errObj,
203
227
  stdout: capturedStdout,
204
- stderr: capturedStderr
228
+ stderr: capturedStderr,
205
229
  };
206
230
 
207
231
  // save results as single line on purpose
@@ -210,7 +234,7 @@ exports.mochaHooks = {
210
234
  g_redis.expire(key, g_expirationTime);
211
235
 
212
236
  // increment passed_count/failed_count & set expiry time
213
- const countKey = `${g_testExecutionId}:${stateFixed}_count`
237
+ const countKey = `${g_testExecutionId}:${stateFixed}_count`;
214
238
  g_redis.incr(countKey);
215
239
  g_redis.expire(countKey, g_expirationTime);
216
240
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mocha-distributed",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "description": "Run multiple mocha suites and tests in parallel, from different processes and different machines. Results available on a redis database.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,7 +28,7 @@
28
28
  "author": "Pau Sanchez",
29
29
  "license": "MIT",
30
30
  "dependencies": {
31
- "redis": "^4.0.2"
31
+ "redis": "^4.0.3"
32
32
  },
33
33
  "devDependencies": {
34
34
  "chai": "^4.2.0",