From c718c0e35af513176630f891eb9b940572ef20c6 Mon Sep 17 00:00:00 2001 From: Outsider Date: Sun, 28 Mar 2021 21:02:38 +0900 Subject: [PATCH 1/2] watch for test files are crashed Signed-off-by: Outsider --- lib/cli/watch-run.js | 26 +++++++++++--------- test/integration/helpers.js | 1 + test/integration/options/watch.spec.js | 34 ++++++++++++++++++++++++++ 3 files changed, 50 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..8ab4bb4649 100644 --- a/test/integration/options/watch.spec.js +++ b/test/integration/options/watch.spec.js @@ -47,6 +47,23 @@ describe('--watch', function() { }); }); + it('reruns test when watched test file is crashed', function() { + const testFile = path.join(tempDir, 'test.js'); + copyFixture(DEFAULT_FIXTURE, testFile); + + return runMochaWatchJSONAsync([testFile], tempDir, () => { + replaceFileContents(testFile, 'done();', 'done((;'); + }) + .then(() => { + 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 +75,23 @@ 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); + + return runMochaWatchJSONAsync(['--parallel', testFile], tempDir, () => { + replaceFileContents(testFile, 'done();', 'done((;'); + }) + .then(() => { + 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() { From b6b4eef2ca90d7bfaaa9f9a275c8478d3880f0b1 Mon Sep 17 00:00:00 2001 From: Outsider Date: Sun, 25 Apr 2021 18:22:20 +0900 Subject: [PATCH 2/2] apply code reviews Signed-off-by: Outsider --- test/integration/options/watch.spec.js | 36 ++++++++++---------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/test/integration/options/watch.spec.js b/test/integration/options/watch.spec.js index 8ab4bb4649..8e76647500 100644 --- a/test/integration/options/watch.spec.js +++ b/test/integration/options/watch.spec.js @@ -47,21 +47,17 @@ describe('--watch', function() { }); }); - it('reruns test when watched test file is crashed', 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(() => { - return runMochaWatchJSONAsync([testFile], tempDir, () => { - replaceFileContents(testFile, 'done((;', 'done();'); - }); - }) - .then(results => { - expect(results, 'to have length', 1); - }); + replaceFileContents(testFile, 'done((;', 'done();'); + }).then(results => { + expect(results, 'to have length', 1); + }); }); describe('when in parallel mode', function() { @@ -80,17 +76,13 @@ describe('--watch', function() { const testFile = path.join(tempDir, 'test.js'); copyFixture(DEFAULT_FIXTURE, testFile); - return runMochaWatchJSONAsync(['--parallel', testFile], tempDir, () => { - replaceFileContents(testFile, 'done();', 'done((;'); - }) - .then(() => { - return runMochaWatchJSONAsync([testFile], tempDir, () => { - replaceFileContents(testFile, 'done((;', 'done();'); - }); - }) - .then(results => { - expect(results, 'to have length', 1); - }); + replaceFileContents(testFile, 'done();', 'done((;'); + + return runMochaWatchJSONAsync([testFile], tempDir, () => { + replaceFileContents(testFile, 'done((;', 'done();'); + }).then(results => { + expect(results, 'to have length', 1); + }); }); });