Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Watch for test files are crashed (#4614)

JeongHoon Byun (a.k.a Outsider)8285910c4d4e
diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.js
index 55932964..dabcd535 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 40b85729..5f338e6c 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 498ddcf5..8e766475 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() {