Skip to content

Commit

Permalink
Convert some classes to ES2015 classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 22, 2016
1 parent 8530fcd commit fa82a24
Show file tree
Hide file tree
Showing 15 changed files with 359 additions and 487 deletions.
13 changes: 5 additions & 8 deletions lib/ava-error.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
'use strict';

function AvaError(message) {
if (!(this instanceof AvaError)) {
throw new TypeError('Class constructor AvaError cannot be invoked without \'new\'');
class AvaError extends Error {
constructor(message) {
super(message);
this.name = 'AvaError';
this.message = message;
}

this.message = message;
this.name = 'AvaError';
}

AvaError.prototype = Object.create(Error.prototype);

module.exports = AvaError;
122 changes: 57 additions & 65 deletions lib/concurrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,80 @@ const isPromise = require('is-promise');
const autoBind = require('auto-bind');
const AvaError = require('./ava-error');

function noop() {}
class Concurrent {
constructor(tests, bail) {
if (!Array.isArray(tests)) {
throw new TypeError('Expected an array of tests');
}

module.exports = Concurrent;
this.results = [];
this.passed = true;
this.reason = null;
this.tests = tests;
this.bail = bail || false;

function Concurrent(tests, bail) {
if (!(this instanceof Concurrent)) {
throw new TypeError('Class constructor Concurrent cannot be invoked without \'new\'');
autoBind(this);
}
run() {
let results;

if (!Array.isArray(tests)) {
throw new TypeError('Expected an array of tests');
}

this.results = [];
this.passed = true;
this.reason = null;
this.tests = tests;
this.bail = bail || false;

autoBind(this);
}

Concurrent.prototype.run = function () {
let results;
try {
results = this.tests.map(this._runTest);
} catch (err) {
if (err instanceof AvaError) {
return this._results();
}

try {
results = this.tests.map(this._runTest);
} catch (err) {
if (err instanceof AvaError) {
return this._results();
throw err;
}

throw err;
}
const isAsync = results.some(isPromise);

const isAsync = results.some(isPromise);
if (isAsync) {
return Promise.all(results)
.catch(AvaError, () => {})
.then(this._results);
}

if (isAsync) {
return Promise.all(results)
.catch(AvaError, noop)
.then(this._results);
return this._results();
}
_runTest(test, index) {
const result = test.run();

return this._results();
};

Concurrent.prototype._runTest = function (test, index) {
const result = test.run();
if (isPromise(result)) {
return result.then(result => this._addResult(result, index));
}

if (isPromise(result)) {
return result.then(result => this._addResult(result, index));
return this._addResult(result, index);
}
_addResult(result, index) {
// always save result when not in bail mode or all previous tests pass
if ((this.bail && this.passed) || !this.bail) {
this.results[index] = result;
}

return this._addResult(result, index);
};
if (result.passed === false) {
this.passed = false;

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

if (result.passed === false) {
this.passed = false;

// only set reason once
if (!this.reason) {
this.reason = result.reason;
if (this.bail) {
throw new AvaError('Error in Concurrent while in bail mode');
}
}

if (this.bail) {
throw new AvaError('Error in Concurrent while in bail mode');
}
return result;
}
_results() {
return {
passed: this.passed,
reason: this.reason,
result: this.results
};
}
}

return result;
};

Concurrent.prototype._results = function () {
return {
passed: this.passed,
reason: this.reason,
result: this.results
};
};
module.exports = Concurrent;
Loading

0 comments on commit fa82a24

Please sign in to comment.