Browse Source

Merge pull request #624 from sindresorhus/watch-and-syntax-errors

Don't crash watch mode when tests files fail to transpile
browser-support
Sindre Sorhus 9 years ago
parent
commit
e810ed4f7d
  1. 42
      api.js
  2. 16
      test/api.js
  3. 5
      test/fixture/syntax-error.js

42
api.js

@ -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) {
@ -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;
}
@ -238,10 +230,13 @@ 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();
@ -249,9 +244,30 @@ Api.prototype.run = function (files) {
}
}
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) {

16
test/api.js

@ -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
test/fixture/syntax-error.js

@ -0,0 +1,5 @@
import test from 'ava';
test.(t => {
t.pass();
});
Loading…
Cancel
Save