From 0b6d8dcb422316d751c73bc1b90dd4ba7ea48c74 Mon Sep 17 00:00:00 2001 From: Thomas Scholtes Date: Tue, 25 Jun 2019 19:46:57 +0200 Subject: [PATCH] Don't re-initialize grep option on watch re-run 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. --- lib/cli/run-helpers.js | 3 +-- lib/cli/watch-run.js | 6 +----- test/integration/options/watch.spec.js | 20 +++++++++++++++++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/cli/run-helpers.js b/lib/cli/run-helpers.js index fce5e6250c..7b3c7aa4fe 100644 --- a/lib/cli/run-helpers.js +++ b/lib/cli/run-helpers.js @@ -118,7 +118,6 @@ exports.runMocha = (mocha, options) => { const { watch = false, extension = [], - grep = '', ui = 'bdd', exit = false, ignore = [], @@ -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); } diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.js index 10d4407673..e7ef34c632 100644 --- a/lib/cli/watch-run.js +++ b/lib/cli/watch-run.js @@ -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); @@ -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); diff --git a/test/integration/options/watch.spec.js b/test/integration/options/watch.spec.js index 53dd9900c3..5d9fb2f26a 100644 --- a/test/integration/options/watch.spec.js +++ b/test/integration/options/watch.spec.js @@ -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); + }); + }); }); }); @@ -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) { @@ -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);