Skip to content

Commit

Permalink
Don't re-initialize grep option on watch re-run (#3960)
Browse files Browse the repository at this point in the history
We remove code that called `mocha.grep(null)` on watch re-runs if the
`--grep` option was not supplied. The code seems to serve no purpose and
is the cause of #2027.
  • Loading branch information
Thomas Scholtes authored and juergba committed Jul 17, 2019
1 parent 5d4dd98 commit 88f45d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
3 changes: 1 addition & 2 deletions lib/cli/run-helpers.js
Expand Up @@ -118,7 +118,6 @@ exports.runMocha = (mocha, options) => {
const {
watch = false,
extension = [],
grep = '',
ui = 'bdd',
exit = false,
ignore = [],
Expand All @@ -138,7 +137,7 @@ exports.runMocha = (mocha, options) => {
};

if (watch) {
watchRun(mocha, {ui, grep}, fileCollectParams);
watchRun(mocha, {ui}, fileCollectParams);
} else {
exports.singleRun(mocha, {exit}, fileCollectParams);
}
Expand Down
6 changes: 1 addition & 5 deletions lib/cli/watch-run.js
Expand Up @@ -16,14 +16,13 @@ const collectFiles = require('./collect-files');
* Run Mocha in "watch" mode
* @param {Mocha} mocha - Mocha instance
* @param {Object} opts - Options
* @param {string|RegExp} opts.grep - Grep for test titles
* @param {string} opts.ui - User interface
* @param {Object} fileCollectParams - Parameters that control test
* file collection. See `lib/cli/collect-files.js`.
* @param {string[]} fileCollectParams.extension - List of extensions to watch
* @private
*/
module.exports = (mocha, {grep, ui}, fileCollectParams) => {
module.exports = (mocha, {ui}, fileCollectParams) => {
let runner;
const files = collectFiles(fileCollectParams);

Expand Down Expand Up @@ -64,9 +63,6 @@ module.exports = (mocha, {grep, ui}, fileCollectParams) => {
const rerun = () => {
purge();
eraseLine();
if (!grep) {
mocha.grep(null);
}
mocha.suite = mocha.suite.clone();
mocha.suite.ctx = new Context();
mocha.ui(ui);
Expand Down
20 changes: 17 additions & 3 deletions test/integration/options/watch.spec.js
Expand Up @@ -115,6 +115,20 @@ describe('--watch', function() {
expect(results[1].failures, 'to have length', 1);
});
});

// Regression test for https://github.com/mochajs/mocha/issues/2027
it('respects --fgrep on re-runs', function() {
const testFile = path.join(this.tempDir, 'test.js');
copyFixture('options/grep', testFile);

return runMochaWatch([testFile, '--fgrep', 'match'], this.tempDir, () => {
touchFile(testFile);
}).then(results => {
expect(results, 'to have length', 2);
expect(results[0].tests, 'to have length', 2);
expect(results[1].tests, 'to have length', 2);
});
});
});
});

Expand Down Expand Up @@ -160,7 +174,7 @@ function touchFile(file) {
}

/**
* Synchronously eplace all substrings matched by `pattern` with
* Synchronously replace all substrings matched by `pattern` with
* `replacement` in the file’s content.
*/
function replaceFileContents(file, pattern, replacement) {
Expand All @@ -170,8 +184,8 @@ function replaceFileContents(file, pattern, replacement) {
}

/**
* Synchronously copy a fixture to the given destion file path. Creates
* parent directories of the destination path if necessary.
* Synchronously copy a fixture to the given destination file path.
* Creates parent directories of the destination path if necessary.
*/
function copyFixture(fixtureName, dest) {
const fixtureSource = helpers.resolveFixturePath(fixtureName);
Expand Down

0 comments on commit 88f45d5

Please sign in to comment.