From 8285910c4d4efc0f1e4a22d14939d103815b7fa5 Mon Sep 17 00:00:00 2001 From: "JeongHoon Byun (a.k.a Outsider)" Date: Mon, 26 Apr 2021 00:31:30 +0900 Subject: [PATCH] Watch for test files are crashed (#4614) Signed-off-by: Outsider --- lib/cli/watch-run.js | 26 +++++++++++++++----------- test/integration/helpers.js | 1 + test/integration/options/watch.spec.js | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.js index 559329647e..dabcd535fb 100644 --- a/lib/cli/watch-run.js +++ b/lib/cli/watch-run.js @@ -261,17 +261,21 @@ const createRerunner = (mocha, watcher, {beforeRun} = {}) => { let rerunScheduled = false; const run = () => { - mocha = beforeRun ? beforeRun({mocha, watcher}) || mocha : mocha; - runner = mocha.run(() => { - debug('finished watch run'); - runner = null; - blastCache(watcher); - if (rerunScheduled) { - rerun(); - } else { - console.error(`${logSymbols.info} [mocha] waiting for changes...`); - } - }); + try { + mocha = beforeRun ? beforeRun({mocha, watcher}) || mocha : mocha; + runner = mocha.run(() => { + debug('finished watch run'); + runner = null; + blastCache(watcher); + if (rerunScheduled) { + rerun(); + } else { + console.error(`${logSymbols.info} [mocha] waiting for changes...`); + } + }); + } catch (e) { + console.error(e.stack); + } }; const scheduleRun = () => { diff --git a/test/integration/helpers.js b/test/integration/helpers.js index 40b8572937..5f338e6c2f 100644 --- a/test/integration/helpers.js +++ b/test/integration/helpers.js @@ -470,6 +470,7 @@ async function runMochaWatchJSONAsync(args, opts, change) { // eslint-disable-next-line no-control-regex .replace(/\u001b\[\?25./g, '') .split('\u001b[2K') + .filter(x => x) .map(x => JSON.parse(x)) ); } diff --git a/test/integration/options/watch.spec.js b/test/integration/options/watch.spec.js index 498ddcf5f9..8e76647500 100644 --- a/test/integration/options/watch.spec.js +++ b/test/integration/options/watch.spec.js @@ -47,6 +47,19 @@ describe('--watch', function() { }); }); + it('reruns test when watched test file crashes', function() { + const testFile = path.join(tempDir, 'test.js'); + copyFixture(DEFAULT_FIXTURE, testFile); + + replaceFileContents(testFile, 'done();', 'done((;'); + + return runMochaWatchJSONAsync([testFile], tempDir, () => { + replaceFileContents(testFile, 'done((;', 'done();'); + }).then(results => { + expect(results, 'to have length', 1); + }); + }); + describe('when in parallel mode', function() { it('reruns test when watched test file is touched', function() { const testFile = path.join(tempDir, 'test.js'); @@ -58,6 +71,19 @@ describe('--watch', function() { expect(results, 'to have length', 2); }); }); + + it('reruns test when watched test file is crashed', function() { + const testFile = path.join(tempDir, 'test.js'); + copyFixture(DEFAULT_FIXTURE, testFile); + + replaceFileContents(testFile, 'done();', 'done((;'); + + return runMochaWatchJSONAsync([testFile], tempDir, () => { + replaceFileContents(testFile, 'done((;', 'done();'); + }).then(results => { + expect(results, 'to have length', 1); + }); + }); }); it('reruns test when file matching --watch-files changes', function() {