Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch for test files are crashed #4614

Merged
merged 2 commits into from Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/cli/watch-run.js
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
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filtered output because the output is an empty string('') when a test file is crashed.

.map(x => JSON.parse(x))
);
}
Expand Down
26 changes: 26 additions & 0 deletions test/integration/options/watch.spec.js
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