Browse Source

minor style tweaks

babel-plugin-for-integration-tests
Sindre Sorhus 9 years ago
parent
commit
ffef6d6231
  1. 1
      api.js
  2. 7
      lib/beautify-stack.js
  3. 6
      lib/caching-precompiler.js
  4. 1
      lib/colors.js
  5. 6
      lib/concurrent.js
  6. 2
      lib/reporters/mini.js
  7. 2
      lib/runner.js
  8. 6
      lib/send.js
  9. 16
      lib/sequence.js
  10. 16
      lib/test-collection.js
  11. 5
      lib/test-worker.js
  12. 17
      lib/test.js

1
api.js

@ -51,6 +51,7 @@ Api.prototype._runFile = function (file) {
var options = objectAssign({}, this.options, {
precompiled: this.precompiler.generateHashForFile(file)
});
return fork(file, options)
.on('stats', this._handleStats)
.on('test', this._handleTest)

7
lib/beautify-stack.js

@ -1,5 +1,4 @@
'use strict';
var StackUtils = require('stack-utils');
var debug = require('debug')('ava');
@ -10,10 +9,8 @@ var stackUtils = new StackUtils({
])
});
function beautifyStack(stack) {
module.exports = function (stack) {
return stack.split('\n')[0] + '\n' + stackUtils.clean(stack).split('\n').map(function (s) {
return ' ' + s;
}).join('\n');
}
module.exports = beautifyStack;
};

6
lib/caching-precompiler.js

@ -1,6 +1,6 @@
var cachingTransform = require('caching-transform');
var fs = require('fs');
var path = require('path');
var cachingTransform = require('caching-transform');
var md5Hex = require('md5-hex');
var stripBom = require('strip-bom');
@ -8,8 +8,9 @@ module.exports = CachingPrecompiler;
function CachingPrecompiler(cacheDir) {
if (!(this instanceof CachingPrecompiler)) {
throw new Error('CachingPrecompiler must be called with new');
throw new TypeError('Class constructor CachingPrecompiler cannot be invoked without \'new\'');
}
this.cacheDir = cacheDir;
this.filenameToHash = {};
this.transform = this._createTransform();
@ -84,6 +85,7 @@ CachingPrecompiler.prototype.precompileFile = function (filename) {
if (!this.filenameToHash[filename]) {
this.transform(stripBom(fs.readFileSync(filename)), filename);
}
return this.filenameToHash[filename];
};

1
lib/colors.js

@ -1,5 +1,4 @@
'use strict';
var chalk = require('chalk');
module.exports = {

6
lib/concurrent.js

@ -9,6 +9,7 @@ function Concurrent(tests, bail) {
if (!this instanceof Concurrent) {
throw new TypeError('Class constructor Concurrent cannot be invoked without \'new\'');
}
this.tests = tests;
this.bail = bail;
}
@ -27,21 +28,25 @@ Concurrent.prototype.run = function () {
if (passed) {
passed = false;
reason = result.reason;
if (bail) {
throw BAIL_ERROR;
}
}
}
return result;
}
for (var i = 0; i < tests.length; i++) {
var result = tests[i].run();
if (isPromise(result)) {
sync = false;
results.push(result.then(addAsync));
} else {
results.push(result);
if (!result.passed) {
if (bail) {
return {
@ -78,6 +83,7 @@ Concurrent.prototype.run = function () {
if (err !== BAIL_ERROR) {
throw err;
}
return {
passed: false,
reason: reason

2
lib/reporters/mini.js

@ -179,6 +179,7 @@ MiniReporter.prototype._update = function (data) {
}
var currentStatus = this.currentStatus;
if (currentStatus.length) {
lastLine = this.lastLineTracker.lastLine();
// We need a newline at the end of the last log line, before the status message.
@ -205,6 +206,7 @@ function eraseLines(count) {
for (var i = 0; i < count; i++) {
clear += ERASE_LINE + (i < count - 1 ? CURSOR_UP : '');
}
if (count) {
clear += CURSOR_TO_COLUMN_0;
}

2
lib/runner.js

@ -47,6 +47,7 @@ optionChain(chainableMethods, function (opts, title, fn) {
fn = title;
title = null;
}
this.tests.add({
metadata: opts,
fn: fn,
@ -57,6 +58,7 @@ optionChain(chainableMethods, function (opts, title, fn) {
Runner.prototype._addTestResult = function (result) {
if (result.result.metadata.type === 'test') {
this.stats.testCount++;
if (result.result.metadata.skipped) {
this.stats.skipCount++;
}

6
lib/send.js

@ -1,7 +1,7 @@
'use strict';
// utility to send messages to processes
function send(ps, name, data) {
module.exports = function (ps, name, data) {
if (typeof ps === 'string') {
data = name || {};
name = ps;
@ -13,6 +13,4 @@ function send(ps, name, data) {
data: data,
ava: true
});
}
module.exports = send;
};

16
lib/sequence.js

@ -8,6 +8,7 @@ function Sequence(tests, bail) {
if (!this instanceof Sequence) {
throw new TypeError('Class constructor Sequence cannot be invoked without \'new\'');
}
this.tests = tests;
this.bail = bail;
}
@ -24,10 +25,13 @@ Sequence.prototype.run = function () {
for (var i = 0; i < len; i++) {
var test = tests[i];
var result = test.run();
if (isPromise(result)) {
return this._runAsync(results, result, tests.slice(i + 1));
}
results.push(result);
if (!result.passed) {
if (bail) {
return {
@ -41,6 +45,7 @@ Sequence.prototype.run = function () {
}
}
}
return {
passed: passed,
reason: reason,
@ -57,11 +62,13 @@ Sequence.prototype._runAsync = function (results, firstAsyncResult, remaining) {
function processResult(result) {
results.push(result);
if (!result.passed) {
if (passed) {
passed = false;
reason = result.reason;
}
if (bail) {
throw BAIL_ERROR;
}
@ -70,19 +77,22 @@ Sequence.prototype._runAsync = function (results, firstAsyncResult, remaining) {
var result = firstAsyncResult.then(function (firstResult) {
processResult(firstResult);
return Promise.each(remaining, function (test) {
var result = test.run();
if (isPromise(result)) {
return result.then(processResult);
}
processResult(result);
});
});
if (bail) {
result = result.catch(function (e) {
if (e !== BAIL_ERROR) {
throw e;
result = result.catch(function (err) {
if (err !== BAIL_ERROR) {
throw err;
}
});
}

16
lib/test-collection.js

@ -3,8 +3,9 @@ module.exports = TestCollection;
function TestCollection() {
if (!(this instanceof TestCollection)) {
throw new Error('must use `new TestCollection()`');
throw new TypeError('Class constructor TestCollection cannot be invoked without \'new\'');
}
this.serial = [];
this.concurrent = [];
this.tests = {
@ -19,24 +20,31 @@ function TestCollection() {
TestCollection.prototype.add = function (test) {
var metadata = test.metadata;
var type = metadata.type;
if (!type) {
throw new Error('test type must be specified');
}
if (type === 'test') {
if (this.hasExclusive && !metadata.exclusive) {
return;
}
if (metadata.exclusive && !this.hasExclusive) {
this.hasExclusive = true;
this.serial = [];
this.concurrent = [];
}
(metadata.serial ? this.serial : this.concurrent).push(test);
return;
}
if (metadata.exclusive) {
throw new Error('you can\'t use "only" with a ' + type + ' test');
}
this.tests[type].push(test);
};
@ -65,9 +73,11 @@ function computeTitle(entry) {
function buildHooks(hookArray, title, contextRef, report) {
return hookArray.map(function (hook) {
var test = buildHook(hook, title, contextRef, report);
if (hook.metadata.skipped) {
return skipRunner(test, report);
}
return test;
});
}
@ -103,9 +113,11 @@ function buildTestWithHooks(entry, beforeEach, afterEach, report) {
if (entry.metadata.skipped) {
return [skipRunner(buildTest(entry), report)];
}
var contextRef = {context: {}};
var arr = buildHooks(beforeEach, entry.title, contextRef, report);
arr.push(buildTest(entry, contextRef, report));
return arr.concat(buildHooks(afterEach, entry.title, contextRef, report));
}
@ -127,6 +139,7 @@ TestCollection.prototype.buildPhases = function (report) {
if (entry.metadata.skipped) {
return skipRunner(entry, report);
}
return buildTest(entry, null, report);
})),
new Sequence([
@ -141,6 +154,7 @@ TestCollection.prototype.buildPhases = function (report) {
if (entry.metadata.skipped) {
return skipRunner(entry, report);
}
return buildTest(entry, null, report);
}))
],

5
lib/test-worker.js

@ -7,12 +7,15 @@ if (opts.tty) {
process.stdout.isTTY = true;
process.stdout.columns = opts.tty.columns || 80;
process.stdout.rows = opts.tty.rows;
var tty = require('tty');
var isatty = tty.isatty;
tty.isatty = function (fd) {
if (fd === 1 || fd === process.stdout) {
return true;
}
return isatty(fd);
};
}
@ -66,10 +69,12 @@ exports.avaRequired = false;
installPrecompiler(function (filename) {
var precompiled = opts.precompiled[filename];
if (precompiled) {
sourceMapCache[filename] = path.join(cacheDir, precompiled + '.map');
return fs.readFileSync(path.join(cacheDir, precompiled + '.js'), 'utf8');
}
return null;
});

17
lib/test.js

@ -61,6 +61,7 @@ Test.prototype._assert = function (promise) {
if (isPromise(promise)) {
this.sync = false;
}
this.assertions.push(promise);
};
@ -90,18 +91,22 @@ Test.prototype.plan = function (count) {
Test.prototype._run = function () {
var ret;
try {
ret = this.fn(this._publicApi());
} catch (err) {
this._setAssertError(err);
}
return ret;
};
Test.prototype.promise = function () {
var self = this;
if (!this._promise) {
this._promise = {};
this._promise.promise = new Promise(function (resolve, reject) { // eslint-disable-line
self._promise.resolve = resolve;
self._promise.reject = reject;
@ -111,6 +116,7 @@ Test.prototype.promise = function () {
}
});
}
return this._promise;
};
@ -158,6 +164,7 @@ Test.prototype.run = function () {
self._setAssertError(err);
self.exit();
});
return this.promise().promise;
}
@ -178,6 +185,7 @@ Object.defineProperty(Test.prototype, 'end', {
if (this.metadata.callback) {
return this._end.bind(this);
}
throw new Error('t.end is not supported in this context. To use t.end as a callback, you must use "callback mode" via `test.cb(testName, fn)` ');
}
});
@ -224,9 +232,11 @@ Test.prototype.exit = function () {
self.duration = globals.now() - self._timeStart;
globals.clearTimeout(self._timeout);
var result = this._result();
if (this.report) {
this.report(result);
}
return result;
}
@ -261,15 +271,18 @@ function PublicApi(test) {
function onAssertionEvent(event) {
var promise = null;
if (event.assertionThrew) {
event.error.powerAssertContext = event.powerAssertContext;
event.error.originalMessage = event.originalMessage;
this._test._setAssertError(event.error);
} else {
var ret = event.returnValue;
if (isObservable(ret)) {
ret = observableToPromise(ret);
}
if (isPromise(ret)) {
promise = ret
.then(null, function (err) {
@ -278,7 +291,9 @@ function onAssertionEvent(event) {
});
}
}
this._test._assert(promise);
return promise;
}
@ -312,10 +327,12 @@ Object.defineProperty(PublicApi.prototype, 'context', {
},
set: function (context) {
var contextRef = this._test.contextRef;
if (!contextRef) {
this._test._setAssertError(new Error('t.context is not available in ' + this._test.metadata.type + ' tests'));
return;
}
contextRef.context = context;
}
});

Loading…
Cancel
Save