Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var fork = require('./lib/fork');
var formatter = require('./lib/enhance-assert').formatter();
var CachingPrecompiler = require('./lib/caching-precompiler');

function Api(files, options) {
function Api(options) {
if (!(this instanceof Api)) {
throw new TypeError('Class constructor Api cannot be invoked without \'new\'');
}
Expand All @@ -28,16 +28,6 @@ function Api(files, options) {
this.options = options || {};
this.options.require = (this.options.require || []).map(resolveCwd);

if (!files || files.length === 0) {
this.files = [
'test.js',
'test-*.js',
'test'
];
} else {
this.files = files;
}

this.excludePatterns = [
'!**/node_modules/**',
'!**/fixtures/**',
Expand Down Expand Up @@ -66,7 +56,6 @@ Api.prototype._reset = function () {
this.stats = [];
this.tests = [];
this.base = '';
this.explicitTitles = false;
};

Api.prototype._runFile = function (file) {
Expand Down Expand Up @@ -136,7 +125,7 @@ Api.prototype._handleTest = function (test) {
};

Api.prototype._prefixTitle = function (file) {
if (this.fileCount === 1 && !this.explicitTitles) {
if (this.fileCount === 1 && !this.options.explicitTitles) {
return '';
}

Expand All @@ -162,8 +151,7 @@ Api.prototype.run = function (files) {
var self = this;

this._reset();
this.explicitTitles = Boolean(files);
return handlePaths(files || this.files, this.excludePatterns)
return handlePaths(files, this.excludePatterns)
.map(function (file) {
return path.resolve(file);
})
Expand Down
19 changes: 15 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ if (cli.flags.init) {
return;
}

var api = new Api(cli.input.length ? cli.input : arrify(conf.files), {
var api = new Api({
failFast: cli.flags.failFast,
serial: cli.flags.serial,
require: arrify(cli.flags.require),
cacheEnabled: cli.flags.cache !== false
cacheEnabled: cli.flags.cache !== false,
explicitTitles: cli.flags.watch
});

var reporter;
Expand All @@ -121,9 +122,19 @@ api.on('error', logger.unhandledError);
api.on('stdout', logger.stdout);
api.on('stderr', logger.stderr);

var files = cli.input.length ? cli.input : arrify(conf.files);

if (files.length === 0) {
files = [
'test.js',
'test-*.js',
'test'
];
}

if (cli.flags.watch) {
try {
watcher.start(logger, api, arrify(cli.flags.source), process.stdin);
watcher.start(logger, api, files, arrify(cli.flags.source), process.stdin);
} catch (err) {
if (err.name === 'AvaError') {
// An AvaError may be thrown if chokidar is not installed. Log it nicely.
Expand All @@ -135,7 +146,7 @@ if (cli.flags.watch) {
}
}
} else {
api.run()
api.run(files)
.then(function () {
logger.finish();
logger.exit(api.failCount > 0 || api.rejectionCount > 0 || api.exceptionCount > 0 ? 1 : 0);
Expand Down
16 changes: 8 additions & 8 deletions lib/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ function getChokidarPatterns(sources, initialFiles) {
return {paths: paths, ignored: ignored};
}

exports.start = function (logger, api, sources, stdin) {
var isTest = makeTestMatcher(api.files, api.excludePatterns);
exports.start = function (logger, api, files, sources, stdin) {
var isTest = makeTestMatcher(files, api.excludePatterns);
var patterns = getChokidarPatterns(sources, files);

var patterns = getChokidarPatterns(sources, api.files);
var watcher = requireChokidar().watch(patterns.paths, {
ignored: patterns.ignored,
ignoreInitial: true
});

var busy = api.run().then(function () {
var busy = api.run(files).then(function () {
logger.finish();
}).catch(rethrowAsync);

Expand Down Expand Up @@ -90,7 +90,7 @@ exports.start = function (logger, api, sources, stdin) {
debounceAgain = false;
debounce();
} else {
busy = runAfterChanges(logger, api, isTest, dirtyStates);
busy = runAfterChanges(logger, api, files, isTest, dirtyStates);
dirtyStates = {};
debouncing = null;
debounceAgain = false;
Expand Down Expand Up @@ -122,7 +122,7 @@ exports.start = function (logger, api, sources, stdin) {
// Cancel the debouncer again, it might have restarted while waiting for
// the busy promise to fulfil.
cancelDebounce();
busy = runAfterChanges(logger, api, isTest, {});
busy = runAfterChanges(logger, api, files, isTest, {});
});
});
};
Expand Down Expand Up @@ -172,7 +172,7 @@ function makeTestMatcher(files, excludePatterns) {
};
}

function runAfterChanges(logger, api, isTest, dirtyStates) {
function runAfterChanges(logger, api, files, isTest, dirtyStates) {
var dirtyPaths = Object.keys(dirtyStates);
var dirtyTests = dirtyPaths.filter(isTest);
var addedOrChangedTests = dirtyTests.filter(function (path) {
Expand All @@ -195,7 +195,7 @@ function runAfterChanges(logger, api, isTest, dirtyStates) {
if (dirtyPaths.length > 0 && dirtyTests.length === dirtyPaths.length) {
resolve(api.run(addedOrChangedTests));
} else {
resolve(api.run());
resolve(api.run(files));
}
}).then(function () {
logger.finish();
Expand Down
Loading