Skip to content

Commit b0b8b65

Browse files
committed
Convert some classes to ES2015 classes
1 parent 8267755 commit b0b8b65

15 files changed

+359
-487
lines changed

lib/ava-error.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
'use strict';
22

3-
function AvaError(message) {
4-
if (!(this instanceof AvaError)) {
5-
throw new TypeError('Class constructor AvaError cannot be invoked without \'new\'');
3+
class AvaError extends Error {
4+
constructor(message) {
5+
super(message);
6+
this.name = 'AvaError';
7+
this.message = message;
68
}
7-
8-
this.message = message;
9-
this.name = 'AvaError';
109
}
1110

12-
AvaError.prototype = Object.create(Error.prototype);
13-
1411
module.exports = AvaError;

lib/concurrent.js

+57-65
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,80 @@ const isPromise = require('is-promise');
44
const autoBind = require('auto-bind');
55
const AvaError = require('./ava-error');
66

7-
function noop() {}
7+
class Concurrent {
8+
constructor(tests, bail) {
9+
if (!Array.isArray(tests)) {
10+
throw new TypeError('Expected an array of tests');
11+
}
812

9-
module.exports = Concurrent;
13+
this.results = [];
14+
this.passed = true;
15+
this.reason = null;
16+
this.tests = tests;
17+
this.bail = bail || false;
1018

11-
function Concurrent(tests, bail) {
12-
if (!(this instanceof Concurrent)) {
13-
throw new TypeError('Class constructor Concurrent cannot be invoked without \'new\'');
19+
autoBind(this);
1420
}
21+
run() {
22+
let results;
1523

16-
if (!Array.isArray(tests)) {
17-
throw new TypeError('Expected an array of tests');
18-
}
19-
20-
this.results = [];
21-
this.passed = true;
22-
this.reason = null;
23-
this.tests = tests;
24-
this.bail = bail || false;
25-
26-
autoBind(this);
27-
}
28-
29-
Concurrent.prototype.run = function () {
30-
let results;
24+
try {
25+
results = this.tests.map(this._runTest);
26+
} catch (err) {
27+
if (err instanceof AvaError) {
28+
return this._results();
29+
}
3130

32-
try {
33-
results = this.tests.map(this._runTest);
34-
} catch (err) {
35-
if (err instanceof AvaError) {
36-
return this._results();
31+
throw err;
3732
}
3833

39-
throw err;
40-
}
34+
const isAsync = results.some(isPromise);
4135

42-
const isAsync = results.some(isPromise);
36+
if (isAsync) {
37+
return Promise.all(results)
38+
.catch(AvaError, () => {})
39+
.then(this._results);
40+
}
4341

44-
if (isAsync) {
45-
return Promise.all(results)
46-
.catch(AvaError, noop)
47-
.then(this._results);
42+
return this._results();
4843
}
44+
_runTest(test, index) {
45+
const result = test.run();
4946

50-
return this._results();
51-
};
52-
53-
Concurrent.prototype._runTest = function (test, index) {
54-
const result = test.run();
47+
if (isPromise(result)) {
48+
return result.then(result => this._addResult(result, index));
49+
}
5550

56-
if (isPromise(result)) {
57-
return result.then(result => this._addResult(result, index));
51+
return this._addResult(result, index);
5852
}
53+
_addResult(result, index) {
54+
// always save result when not in bail mode or all previous tests pass
55+
if ((this.bail && this.passed) || !this.bail) {
56+
this.results[index] = result;
57+
}
5958

60-
return this._addResult(result, index);
61-
};
59+
if (result.passed === false) {
60+
this.passed = false;
6261

63-
Concurrent.prototype._addResult = function (result, index) {
64-
// always save result when not in bail mode or all previous tests pass
65-
if ((this.bail && this.passed) || !this.bail) {
66-
this.results[index] = result;
67-
}
62+
// only set reason once
63+
if (!this.reason) {
64+
this.reason = result.reason;
65+
}
6866

69-
if (result.passed === false) {
70-
this.passed = false;
71-
72-
// only set reason once
73-
if (!this.reason) {
74-
this.reason = result.reason;
67+
if (this.bail) {
68+
throw new AvaError('Error in Concurrent while in bail mode');
69+
}
7570
}
7671

77-
if (this.bail) {
78-
throw new AvaError('Error in Concurrent while in bail mode');
79-
}
72+
return result;
8073
}
74+
_results() {
75+
return {
76+
passed: this.passed,
77+
reason: this.reason,
78+
result: this.results
79+
};
80+
}
81+
}
8182

82-
return result;
83-
};
84-
85-
Concurrent.prototype._results = function () {
86-
return {
87-
passed: this.passed,
88-
reason: this.reason,
89-
result: this.results
90-
};
91-
};
83+
module.exports = Concurrent;

0 commit comments

Comments
 (0)