Skip to content

Commit

Permalink
Merge pull request #624 from sindresorhus/watch-and-syntax-errors
Browse files Browse the repository at this point in the history
Don't crash watch mode when tests files fail to transpile
  • Loading branch information
sindresorhus committed Mar 10, 2016
2 parents f1966d9 + 768ef9d commit e810ed4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
42 changes: 29 additions & 13 deletions api.js
Expand Up @@ -192,11 +192,7 @@ Api.prototype.run = function (files) {
self.fileCount = files.length;
self.base = path.relative('.', commonPathPrefix(files)) + path.sep;

var tests = files.map(self._runFile);

// receive test count from all files and then run the tests
var unreportedFiles = self.fileCount;

var tests = new Array(self.fileCount);
return new Promise(function (resolve) {
function run() {
if (self.options.match.length > 0 && !self.hasExclusive) {
Expand All @@ -205,10 +201,6 @@ Api.prototype.run = function (files) {
file: undefined
});

tests.forEach(function (test) {
// No tests will be run so tear down the child processes.
test.send('teardown');
});
resolve([]);
return;
}
Expand Down Expand Up @@ -238,20 +230,44 @@ Api.prototype.run = function (files) {
}));
}

tests.forEach(function (test) {
// receive test count from all files and then run the tests
var unreportedFiles = self.fileCount;
var bailed = false;
files.every(function (file, index) {
var tried = false;
function tryRun() {
if (!tried) {
if (!tried && !bailed) {
unreportedFiles--;
if (unreportedFiles === 0) {
run();
}
}
}

test.on('stats', tryRun);
test.catch(tryRun);
try {
var test = tests[index] = self._runFile(file);
test.on('stats', tryRun);
test.catch(tryRun);
return true;
} catch (err) {
bailed = true;
self._handleExceptions({
exception: err,
file: file
});
resolve([]);
return false;
}
});
}).then(function (results) {
if (results.length === 0) {
// No tests ran, make sure to tear down the child processes.
tests.forEach(function (test) {
test.send('teardown');
});
}

return results;
});
})
.then(function (results) {
Expand Down
16 changes: 16 additions & 0 deletions test/api.js
Expand Up @@ -730,3 +730,19 @@ test('using --match with no matching tests causes an AvaError to be emitted', fu

return api.run([path.join(__dirname, 'fixture/match-no-match.js')]);
});

test('errors thrown when running files are emitted', function (t) {
t.plan(2);

var api = new Api();

api.on('error', function (err) {
t.is(err.name, 'SyntaxError');
t.match(err.message, /Unexpected token/);
});

return api.run([
path.join(__dirname, 'fixture/es2015.js'),
path.join(__dirname, 'fixture/syntax-error.js')
]);
});
5 changes: 5 additions & 0 deletions test/fixture/syntax-error.js
@@ -0,0 +1,5 @@
import test from 'ava';

test.(t => {
t.pass();
});

0 comments on commit e810ed4

Please sign in to comment.