Skip to content

Commit 768ef9d

Browse files
committed
handle exceptions when initially running files
Catch exceptions when initially running files. Don't run any tests, just report the exception. Move test teardown into a callback for the run promise so it can be used when --match is used but there are no matches, and when exceptions occur while initially running files. Teardown causes the test promise to reject, which leads to an attempt to run the tests. Add a guard to prevent this. Fixes #622.
1 parent 901c48c commit 768ef9d

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

api.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ Api.prototype.run = function (files) {
192192
self.fileCount = files.length;
193193
self.base = path.relative('.', commonPathPrefix(files)) + path.sep;
194194

195-
var tests = files.map(self._runFile);
196-
195+
var tests = new Array(self.fileCount);
197196
return new Promise(function (resolve) {
198197
function run() {
199198
if (self.options.match.length > 0 && !self.hasExclusive) {
@@ -202,10 +201,6 @@ Api.prototype.run = function (files) {
202201
file: undefined
203202
});
204203

205-
tests.forEach(function (test) {
206-
// No tests will be run so tear down the child processes.
207-
test.send('teardown');
208-
});
209204
resolve([]);
210205
return;
211206
}
@@ -237,20 +232,42 @@ Api.prototype.run = function (files) {
237232

238233
// receive test count from all files and then run the tests
239234
var unreportedFiles = self.fileCount;
240-
tests.forEach(function (test) {
235+
var bailed = false;
236+
files.every(function (file, index) {
241237
var tried = false;
242238
function tryRun() {
243-
if (!tried) {
239+
if (!tried && !bailed) {
244240
unreportedFiles--;
245241
if (unreportedFiles === 0) {
246242
run();
247243
}
248244
}
249245
}
250246

251-
test.on('stats', tryRun);
252-
test.catch(tryRun);
247+
try {
248+
var test = tests[index] = self._runFile(file);
249+
test.on('stats', tryRun);
250+
test.catch(tryRun);
251+
return true;
252+
} catch (err) {
253+
bailed = true;
254+
self._handleExceptions({
255+
exception: err,
256+
file: file
257+
});
258+
resolve([]);
259+
return false;
260+
}
253261
});
262+
}).then(function (results) {
263+
if (results.length === 0) {
264+
// No tests ran, make sure to tear down the child processes.
265+
tests.forEach(function (test) {
266+
test.send('teardown');
267+
});
268+
}
269+
270+
return results;
254271
});
255272
})
256273
.then(function (results) {

test/api.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,3 +730,19 @@ test('using --match with no matching tests causes an AvaError to be emitted', fu
730730

731731
return api.run([path.join(__dirname, 'fixture/match-no-match.js')]);
732732
});
733+
734+
test('errors thrown when running files are emitted', function (t) {
735+
t.plan(2);
736+
737+
var api = new Api();
738+
739+
api.on('error', function (err) {
740+
t.is(err.name, 'SyntaxError');
741+
t.match(err.message, /Unexpected token/);
742+
});
743+
744+
return api.run([
745+
path.join(__dirname, 'fixture/es2015.js'),
746+
path.join(__dirname, 'fixture/syntax-error.js')
747+
]);
748+
});

test/fixture/syntax-error.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from 'ava';
2+
3+
test.(t => {
4+
t.pass();
5+
});

0 commit comments

Comments
 (0)