Skip to content

Commit

Permalink
Watch for test files are crashed (#4614)
Browse files Browse the repository at this point in the history
Signed-off-by: Outsider <[email protected]>
  • Loading branch information
outsideris authored Apr 25, 2021
1 parent 34643e4 commit 8285910
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/cli/watch-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
1 change: 1 addition & 0 deletions test/integration/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}
Expand Down
26 changes: 26 additions & 0 deletions test/integration/options/watch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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() {
Expand Down

0 comments on commit 8285910

Please sign in to comment.