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, { var options = objectAssign({}, this.options, {
precompiled: this.precompiler.generateHashForFile(file) precompiled: this.precompiler.generateHashForFile(file)
}); });
return fork(file, options) return fork(file, options)
.on('stats', this._handleStats) .on('stats', this._handleStats)
.on('test', this._handleTest) .on('test', this._handleTest)

7
lib/beautify-stack.js

@ -1,5 +1,4 @@
'use strict'; 'use strict';
var StackUtils = require('stack-utils'); var StackUtils = require('stack-utils');
var debug = require('debug')('ava'); 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 stack.split('\n')[0] + '\n' + stackUtils.clean(stack).split('\n').map(function (s) {
return ' ' + s; return ' ' + s;
}).join('\n'); }).join('\n');
} };
module.exports = beautifyStack;

6
lib/caching-precompiler.js

@ -1,6 +1,6 @@
var cachingTransform = require('caching-transform');
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var cachingTransform = require('caching-transform');
var md5Hex = require('md5-hex'); var md5Hex = require('md5-hex');
var stripBom = require('strip-bom'); var stripBom = require('strip-bom');
@ -8,8 +8,9 @@ module.exports = CachingPrecompiler;
function CachingPrecompiler(cacheDir) { function CachingPrecompiler(cacheDir) {
if (!(this instanceof CachingPrecompiler)) { 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.cacheDir = cacheDir;
this.filenameToHash = {}; this.filenameToHash = {};
this.transform = this._createTransform(); this.transform = this._createTransform();
@ -84,6 +85,7 @@ CachingPrecompiler.prototype.precompileFile = function (filename) {
if (!this.filenameToHash[filename]) { if (!this.filenameToHash[filename]) {
this.transform(stripBom(fs.readFileSync(filename)), filename); this.transform(stripBom(fs.readFileSync(filename)), filename);
} }
return this.filenameToHash[filename]; return this.filenameToHash[filename];
}; };

1
lib/colors.js

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

6
lib/concurrent.js

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

2
lib/reporters/mini.js

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

2
lib/runner.js

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

6
lib/send.js

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

16
lib/sequence.js

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

16
lib/test-collection.js

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

5
lib/test-worker.js

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

17
lib/test.js

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

Loading…
Cancel
Save