Skip to content

Commit 8285910

Browse files
authored
Watch for test files are crashed (#4614)
Signed-off-by: Outsider <outsideris@gmail.com>
1 parent 34643e4 commit 8285910

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

lib/cli/watch-run.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,21 @@ const createRerunner = (mocha, watcher, {beforeRun} = {}) => {
261261
let rerunScheduled = false;
262262

263263
const run = () => {
264-
mocha = beforeRun ? beforeRun({mocha, watcher}) || mocha : mocha;
265-
runner = mocha.run(() => {
266-
debug('finished watch run');
267-
runner = null;
268-
blastCache(watcher);
269-
if (rerunScheduled) {
270-
rerun();
271-
} else {
272-
console.error(`${logSymbols.info} [mocha] waiting for changes...`);
273-
}
274-
});
264+
try {
265+
mocha = beforeRun ? beforeRun({mocha, watcher}) || mocha : mocha;
266+
runner = mocha.run(() => {
267+
debug('finished watch run');
268+
runner = null;
269+
blastCache(watcher);
270+
if (rerunScheduled) {
271+
rerun();
272+
} else {
273+
console.error(`${logSymbols.info} [mocha] waiting for changes...`);
274+
}
275+
});
276+
} catch (e) {
277+
console.error(e.stack);
278+
}
275279
};
276280

277281
const scheduleRun = () => {

test/integration/helpers.js

+1
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ async function runMochaWatchJSONAsync(args, opts, change) {
470470
// eslint-disable-next-line no-control-regex
471471
.replace(/\u001b\[\?25./g, '')
472472
.split('\u001b[2K')
473+
.filter(x => x)
473474
.map(x => JSON.parse(x))
474475
);
475476
}

test/integration/options/watch.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ describe('--watch', function() {
4747
});
4848
});
4949

50+
it('reruns test when watched test file crashes', function() {
51+
const testFile = path.join(tempDir, 'test.js');
52+
copyFixture(DEFAULT_FIXTURE, testFile);
53+
54+
replaceFileContents(testFile, 'done();', 'done((;');
55+
56+
return runMochaWatchJSONAsync([testFile], tempDir, () => {
57+
replaceFileContents(testFile, 'done((;', 'done();');
58+
}).then(results => {
59+
expect(results, 'to have length', 1);
60+
});
61+
});
62+
5063
describe('when in parallel mode', function() {
5164
it('reruns test when watched test file is touched', function() {
5265
const testFile = path.join(tempDir, 'test.js');
@@ -58,6 +71,19 @@ describe('--watch', function() {
5871
expect(results, 'to have length', 2);
5972
});
6073
});
74+
75+
it('reruns test when watched test file is crashed', function() {
76+
const testFile = path.join(tempDir, 'test.js');
77+
copyFixture(DEFAULT_FIXTURE, testFile);
78+
79+
replaceFileContents(testFile, 'done();', 'done((;');
80+
81+
return runMochaWatchJSONAsync([testFile], tempDir, () => {
82+
replaceFileContents(testFile, 'done((;', 'done();');
83+
}).then(results => {
84+
expect(results, 'to have length', 1);
85+
});
86+
});
6187
});
6288

6389
it('reruns test when file matching --watch-files changes', function() {

0 commit comments

Comments
 (0)