diff --git a/api.js b/api.js index da1db9c..6c46a5c 100644 --- a/api.js +++ b/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) { diff --git a/test/api.js b/test/api.js index 36bd811..a2fdb40 100644 --- a/test/api.js +++ b/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') + ]); +}); diff --git a/test/fixture/syntax-error.js b/test/fixture/syntax-error.js new file mode 100644 index 0000000..b44af4a --- /dev/null +++ b/test/fixture/syntax-error.js @@ -0,0 +1,5 @@ +import test from 'ava'; + +test.(t => { + t.pass(); +});