Permalink
Browse files

More ES2015ification

  • Loading branch information...
1 parent 410cb8d commit 61cdd0941517a08b6aa526f623ba8ce6957e7783 @sindresorhus sindresorhus committed Jan 7, 2017
Showing with 969 additions and 1,050 deletions.
  1. +275 −314 api.js
  2. +1 −2 lib/reporters/mini.js
  3. +142 −157 lib/test-collection.js
  4. +276 −297 lib/test.js
  5. +0 −1 package.json
  6. +266 −268 test/api.js
  7. +2 −5 test/test-collection.js
  8. +7 −6 test/test.js
View
Oops, something went wrong.
@@ -8,7 +8,6 @@ var chalk = require('chalk');
var cliTruncate = require('cli-truncate');
var cross = require('figures').cross;
var repeating = require('repeating');
-var objectAssign = require('object-assign');
var colors = require('../colors');
chalk.enabled = true;
@@ -27,7 +26,7 @@ function MiniReporter(options) {
});
this.spinnerInterval = spinnerDef.interval;
- this.options = objectAssign({}, options);
+ this.options = Object.assign({}, options);
this.reset();
this.stream = process.stderr;
View
@@ -1,196 +1,181 @@
'use strict';
-var EventEmitter = require('events').EventEmitter;
-var util = require('util');
-var fnName = require('fn-name');
-var Concurrent = require('./concurrent');
-var Sequence = require('./sequence');
-var Test = require('./test');
-
-module.exports = TestCollection;
-
-function TestCollection() {
- if (!(this instanceof TestCollection)) {
- throw new TypeError('Class constructor TestCollection cannot be invoked without \'new\'');
- }
-
- EventEmitter.call(this);
+const EventEmitter = require('events');
+const fnName = require('fn-name');
+const Concurrent = require('./concurrent');
+const Sequence = require('./sequence');
+const Test = require('./test');
+
+class TestCollection extends EventEmitter {
+ constructor() {
+ super();
+
+ this.hasExclusive = false;
+
+ this.tests = {
+ concurrent: [],
+ serial: []
+ };
+
+ this.hooks = {
+ before: [],
+ beforeEach: [],
+ after: [],
+ afterAlways: [],
+ afterEach: [],
+ afterEachAlways: []
+ };
+
+ this._emitTestResult = this._emitTestResult.bind(this);
+ }
+ add(test) {
+ const metadata = test.metadata;
+ const type = metadata.type;
+
+ if (!type) {
+ throw new Error('Test type must be specified');
+ }
- this.hasExclusive = false;
- this.tests = {
- concurrent: [],
- serial: []
- };
+ if (!test.title && test.fn) {
+ test.title = fnName(test.fn);
+ }
- this.hooks = {
- before: [],
- beforeEach: [],
- after: [],
- afterAlways: [],
- afterEach: [],
- afterEachAlways: []
- };
+ // Workaround for Babel giving anonymous functions a name
+ if (test.title === 'callee$0$0') {
+ test.title = null;
+ }
- this._emitTestResult = this._emitTestResult.bind(this);
-}
+ if (!test.title) {
+ if (type === 'test') {
+ test.title = '[anonymous]';
+ } else {
+ test.title = type;
+ }
+ }
-util.inherits(TestCollection, EventEmitter);
+ if (metadata.always && type !== 'after' && type !== 'afterEach') {
+ throw new Error('"always" can only be used with after and afterEach hooks');
+ }
-TestCollection.prototype.add = function (test) {
- var metadata = test.metadata;
- var type = metadata.type;
+ // Add a hook
+ if (type !== 'test') {
+ if (metadata.exclusive) {
+ throw new Error(`"only" cannot be used with a ${type} hook`);
+ }
- if (!type) {
- throw new Error('Test type must be specified');
- }
+ this.hooks[type + (metadata.always ? 'Always' : '')].push(test);
+ return;
+ }
- if (!test.title && test.fn) {
- test.title = fnName(test.fn);
- }
+ // Add `.only()` tests if `.only()` was used previously
+ if (this.hasExclusive && !metadata.exclusive) {
+ return;
+ }
- // Workaround for Babel giving anonymous functions a name
- if (test.title === 'callee$0$0') {
- test.title = null;
- }
+ if (metadata.exclusive && !this.hasExclusive) {
+ this.tests.concurrent = [];
+ this.tests.serial = [];
+ this.hasExclusive = true;
+ }
- if (!test.title) {
- if (type === 'test') {
- test.title = '[anonymous]';
+ if (metadata.serial) {
+ this.tests.serial.push(test);
} else {
- test.title = type;
+ this.tests.concurrent.push(test);
}
}
+ _skippedTest(test) {
+ const self = this;
- if (metadata.always && type !== 'after' && type !== 'afterEach') {
- throw new Error('"always" can only be used with after and afterEach hooks');
- }
+ return {
+ run() {
+ const result = {
+ passed: true,
+ result: test
+ };
- // Add a hook
- if (type !== 'test') {
- if (metadata.exclusive) {
- throw new Error('"only" cannot be used with a ' + type + ' hook');
- }
+ self._emitTestResult(result);
- this.hooks[type + (metadata.always ? 'Always' : '')].push(test);
- return;
+ return result;
+ }
+ };
}
-
- // Add .only() tests if .only() was used previously
- if (this.hasExclusive && !metadata.exclusive) {
- return;
+ _emitTestResult(test) {
+ this.emit('test', test);
}
+ _buildHooks(hooks, testTitle, context) {
+ return hooks.map(hook => {
+ const test = this._buildHook(hook, testTitle, context);
- if (metadata.exclusive && !this.hasExclusive) {
- this.tests.concurrent = [];
- this.tests.serial = [];
- this.hasExclusive = true;
- }
+ if (hook.metadata.skipped || hook.metadata.todo) {
+ return this._skippedTest(test);
+ }
- if (metadata.serial) {
- this.tests.serial.push(test);
- } else {
- this.tests.concurrent.push(test);
+ return test;
+ });
}
-};
-
-TestCollection.prototype._skippedTest = function (test) {
- var self = this;
+ _buildHook(hook, testTitle, context) {
+ let title = hook.title;
- return {
- run: function () {
- var result = {
- passed: true,
- result: test
- };
-
- self._emitTestResult(result);
-
- return result;
+ if (testTitle) {
+ title += ` for ${testTitle}`;
}
- };
-};
-
-TestCollection.prototype._emitTestResult = function (test) {
- this.emit('test', test);
-};
-TestCollection.prototype._buildHooks = function (hooks, testTitle, context) {
- return hooks.map(function (hook) {
- var test = this._buildHook(hook, testTitle, context);
-
- if (hook.metadata.skipped || hook.metadata.todo) {
- return this._skippedTest(test);
+ if (!context) {
+ context = null;
}
- return test;
- }, this);
-};
-
-TestCollection.prototype._buildHook = function (hook, testTitle, context) {
- var title = hook.title;
+ const test = new Test(title, hook.fn, context, this._emitTestResult);
+ test.metadata = hook.metadata;
- if (testTitle) {
- title += ' for ' + testTitle;
- }
-
- if (!context) {
- context = null;
+ return test;
}
+ _buildTest(test, context) {
+ if (!context) {
+ context = null;
+ }
- var test = new Test(title, hook.fn, context, this._emitTestResult);
- test.metadata = hook.metadata;
+ const metadata = test.metadata;
- return test;
-};
+ test = new Test(test.title, test.fn, context, this._emitTestResult);
+ test.metadata = metadata;
-TestCollection.prototype._buildTest = function (test, context) {
- if (!context) {
- context = null;
+ return test;
}
+ _buildTestWithHooks(test) {
+ if (test.metadata.skipped) {
+ return new Sequence([this._skippedTest(this._buildTest(test))], true);
+ }
- var metadata = test.metadata;
-
- test = new Test(test.title, test.fn, context, this._emitTestResult);
- test.metadata = metadata;
+ const context = {context: {}};
- return test;
-};
+ const beforeHooks = this._buildHooks(this.hooks.beforeEach, test.title, context);
+ const afterHooks = this._buildHooks(this.hooks.afterEach, test.title, context);
-TestCollection.prototype._buildTestWithHooks = function (test) {
- if (test.metadata.skipped) {
- return new Sequence([this._skippedTest(this._buildTest(test))], true);
+ let sequence = new Sequence([].concat(beforeHooks, this._buildTest(test, context), afterHooks), true);
+ if (this.hooks.afterEachAlways.length > 0) {
+ const afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterEachAlways, test.title, context));
+ sequence = new Sequence([sequence, afterAlwaysHooks], false);
+ }
+ return sequence;
}
+ _buildTests(tests) {
+ return tests.map(test => this._buildTestWithHooks(test));
+ }
+ build(bail) {
+ const beforeHooks = new Sequence(this._buildHooks(this.hooks.before));
+ const afterHooks = new Sequence(this._buildHooks(this.hooks.after));
- var context = {context: {}};
-
- var beforeHooks = this._buildHooks(this.hooks.beforeEach, test.title, context);
- var afterHooks = this._buildHooks(this.hooks.afterEach, test.title, context);
+ const serialTests = new Sequence(this._buildTests(this.tests.serial), bail);
+ const concurrentTests = new Concurrent(this._buildTests(this.tests.concurrent), bail);
+ const allTests = new Sequence([serialTests, concurrentTests]);
- var sequence = new Sequence([].concat(beforeHooks, this._buildTest(test, context), afterHooks), true);
- if (this.hooks.afterEachAlways.length !== 0) {
- var afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterEachAlways, test.title, context));
- sequence = new Sequence([sequence, afterAlwaysHooks], false);
- }
- return sequence;
-};
-
-TestCollection.prototype._buildTests = function (tests) {
- return tests.map(function (test) {
- return this._buildTestWithHooks(test);
- }, this);
-};
-
-TestCollection.prototype.build = function (bail) {
- var beforeHooks = new Sequence(this._buildHooks(this.hooks.before));
- var afterHooks = new Sequence(this._buildHooks(this.hooks.after));
-
- var serialTests = new Sequence(this._buildTests(this.tests.serial), bail);
- var concurrentTests = new Concurrent(this._buildTests(this.tests.concurrent), bail);
- var allTests = new Sequence([serialTests, concurrentTests]);
-
- var finalTests = new Sequence([beforeHooks, allTests, afterHooks], true);
- if (this.hooks.afterAlways.length !== 0) {
- var afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterAlways));
- finalTests = new Sequence([finalTests, afterAlwaysHooks], false);
+ let finalTests = new Sequence([beforeHooks, allTests, afterHooks], true);
+ if (this.hooks.afterAlways.length > 0) {
+ const afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterAlways));
+ finalTests = new Sequence([finalTests, afterAlwaysHooks], false);
+ }
+ return finalTests;
}
- return finalTests;
-};
+}
+
+module.exports = TestCollection;
Oops, something went wrong.

0 comments on commit 61cdd09

Please sign in to comment.