From aab855538fe23720f59f577b4f71d18c5298e4aa Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Fri, 13 Dec 2019 03:41:07 -0800 Subject: [PATCH] Type "rs\n" to restart tests, fixes #871 (#3979) * Type "rs\n" to restart tests, fixes #871 Although the `--watch` feature is somewhat controversial (see #1780), is there any reason not to make it a little easier to use in the meantime? This adds nodemon's shortcut (`rs\n`) for manually restarting. * fix lint errors, add unit test * write to mochaProcess.stdin * Trigger travis build * add documentation * restore trailing whitespace * empty commit (to rerun tests) --- README.md | 8 ++++---- docs/index.md | 2 ++ lib/cli/watch-run.js | 11 +++++++++++ test/integration/options/watch.spec.js | 15 +++++++++++++-- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6e08396307..3dc8ac25d0 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ ## Sponsors -Does your company use Mocha? Ask your manager or marketing team if your company would be interested in supporting our project. Support will allow the maintainers to dedicate more time for maintenance and new features for everyone. Also, your company's logo will show [on GitHub](https://github.com/mochajs/mocha#readme) and on [our site](https://mochajs.org) - who doesn't want a little extra exposure? [Here's the info](https://opencollective.com/mochajs#sponsor). +Does your company use Mocha? Ask your manager or marketing team if your company would be interested in supporting our project. Support will allow the maintainers to dedicate more time for maintenance and new features for everyone. Also, your company's logo will show [on GitHub](https://github.com/mochajs/mocha#readme) and on [our site](https://mochajs.org) - who doesn't want a little extra exposure? [Here's the info](https://opencollective.com/mochajs#sponsor). [![MochaJS Sponsor](https://opencollective.com/mochajs/sponsor/0/avatar)](https://opencollective.com/mochajs/sponsor/0/website) [![MochaJS Sponsor](https://opencollective.com/mochajs/sponsor/1/avatar)](https://opencollective.com/mochajs/sponsor/1/website) @@ -83,12 +83,12 @@ Does your company use Mocha? Ask your manager or marketing team if your company You might want to know that: -- Mocha is the *most-depended-upon* module on npm (source: [libraries.io](https://libraries.io/search?order=desc&platforms=NPM&sort=dependents_count)), and -- Mocha is an *independent* open-source project, maintained exclusively by volunteers. +- Mocha is the _most-depended-upon_ module on npm (source: [libraries.io](https://libraries.io/search?order=desc&platforms=NPM&sort=dependents_count)), and +- Mocha is an _independent_ open-source project, maintained exclusively by volunteers. You might want to help: -- New to contributing to Mocha? Check out this list of [good first issues](https://github.com/mochajs/mocha/issues?q=is%3Aissue+is%3Aopen+label%3Agood-first-issue) +- New to contributing to Mocha? Check out this list of [good first issues](https://github.com/mochajs/mocha/issues?q=is%3Aissue+is%3Aopen+label%3Agood-first-issue) - Mocha could use a hand with [these issues](https://github.com/mochajs/mocha/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) - The [maintainer's handbook](https://github.com/mochajs/mocha/blob/master/MAINTAINERS.md) explains how things get done diff --git a/docs/index.md b/docs/index.md index d3d53a0e94..788dbf1e52 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1137,6 +1137,8 @@ Rerun tests on file changes. The `--watch-files` and `--watch-ignore` options can be used to control which files are watched for changes. +Tests may be rerun manually by typing ⓡ ⓢ ⏎ (same shortcut as `nodemon`). + ### `--watch-files ` > _New in v7.0.0_ diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.js index dbb66ca4d4..b35a906959 100644 --- a/lib/cli/watch-run.js +++ b/lib/cli/watch-run.js @@ -63,6 +63,17 @@ module.exports = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => { console.log('\n'); process.exit(128 + 2); }); + + // Keyboard shortcut for restarting when "rs\n" is typed (ala Nodemon) + process.stdin.resume(); + process.stdin.setEncoding('utf8'); + process.stdin.on('data', data => { + const str = data + .toString() + .trim() + .toLowerCase(); + if (str === 'rs') rerunner.scheduleRun(); + }); }; /** diff --git a/test/integration/options/watch.spec.js b/test/integration/options/watch.spec.js index 0843380990..f5cd382dee 100644 --- a/test/integration/options/watch.spec.js +++ b/test/integration/options/watch.spec.js @@ -137,6 +137,17 @@ describe('--watch', function() { }); }); + it('reruns when "rs\\n" typed', function() { + const testFile = path.join(this.tempDir, 'test.js'); + copyFixture('__default__', testFile); + + return runMochaWatch([testFile], this.tempDir, mochaProcess => { + mochaProcess.stdin.write('rs\n'); + }).then(results => { + expect(results, 'to have length', 2); + }); + }); + it('reruns test when file starting with . and matching --extension is changed', function() { const testFile = path.join(this.tempDir, 'test.js'); copyFixture('__default__', testFile); @@ -282,11 +293,11 @@ describe('--watch', function() { function runMochaWatch(args, cwd, change) { const [mochaProcess, resultPromise] = helpers.invokeMochaAsync( [...args, '--watch', '--reporter', 'json'], - {cwd} + {cwd, stdio: 'pipe'} ); return sleep(1000) - .then(() => change()) + .then(() => change(mochaProcess)) .then(() => sleep(1000)) .then(() => { mochaProcess.kill('SIGINT');