Skip to content

Commit e810ed4

Browse files
committed
Merge pull request #624 from sindresorhus/watch-and-syntax-errors
Don't crash watch mode when tests files fail to transpile
2 parents f1966d9 + 768ef9d commit e810ed4

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

api.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +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-
197-
// receive test count from all files and then run the tests
198-
var unreportedFiles = self.fileCount;
199-
195+
var tests = new Array(self.fileCount);
200196
return new Promise(function (resolve) {
201197
function run() {
202198
if (self.options.match.length > 0 && !self.hasExclusive) {
@@ -205,10 +201,6 @@ Api.prototype.run = function (files) {
205201
file: undefined
206202
});
207203

208-
tests.forEach(function (test) {
209-
// No tests will be run so tear down the child processes.
210-
test.send('teardown');
211-
});
212204
resolve([]);
213205
return;
214206
}
@@ -238,20 +230,44 @@ Api.prototype.run = function (files) {
238230
}));
239231
}
240232

241-
tests.forEach(function (test) {
233+
// receive test count from all files and then run the tests
234+
var unreportedFiles = self.fileCount;
235+
var bailed = false;
236+
files.every(function (file, index) {
242237
var tried = false;
243238
function tryRun() {
244-
if (!tried) {
239+
if (!tried && !bailed) {
245240
unreportedFiles--;
246241
if (unreportedFiles === 0) {
247242
run();
248243
}
249244
}
250245
}
251246

252-
test.on('stats', tryRun);
253-
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+
}
254261
});
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;
255271
});
256272
})
257273
.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)