Skip to content

Commit aab8555

Browse files
broofacraigtaub
authored andcommitted
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)
1 parent 03b58f2 commit aab8555

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
## Sponsors
5858

59-
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).
59+
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).
6060

6161
[![MochaJS Sponsor](https://opencollective.com/mochajs/sponsor/0/avatar)](https://opencollective.com/mochajs/sponsor/0/website)
6262
[![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
8383

8484
You might want to know that:
8585

86-
- Mocha is the *most-depended-upon* module on npm (source: [libraries.io](https://libraries.io/search?order=desc&platforms=NPM&sort=dependents_count)), and
87-
- Mocha is an *independent* open-source project, maintained exclusively by volunteers.
86+
- Mocha is the _most-depended-upon_ module on npm (source: [libraries.io](https://libraries.io/search?order=desc&platforms=NPM&sort=dependents_count)), and
87+
- Mocha is an _independent_ open-source project, maintained exclusively by volunteers.
8888

8989
You might want to help:
9090

91-
- 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)
91+
- 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)
9292
- Mocha could use a hand with [these issues](https://github.com/mochajs/mocha/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22)
9393
- The [maintainer's handbook](https://github.com/mochajs/mocha/blob/master/MAINTAINERS.md) explains how things get done
9494

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,8 @@ Rerun tests on file changes.
11371137

11381138
The `--watch-files` and `--watch-ignore` options can be used to control which files are watched for changes.
11391139

1140+
Tests may be rerun manually by typing ⓡ ⓢ ⏎ (same shortcut as `nodemon`).
1141+
11401142
### `--watch-files <file|directory|glob>`
11411143

11421144
> _New in v7.0.0_

lib/cli/watch-run.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ module.exports = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => {
6363
console.log('\n');
6464
process.exit(128 + 2);
6565
});
66+
67+
// Keyboard shortcut for restarting when "rs\n" is typed (ala Nodemon)
68+
process.stdin.resume();
69+
process.stdin.setEncoding('utf8');
70+
process.stdin.on('data', data => {
71+
const str = data
72+
.toString()
73+
.trim()
74+
.toLowerCase();
75+
if (str === 'rs') rerunner.scheduleRun();
76+
});
6677
};
6778

6879
/**

test/integration/options/watch.spec.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ describe('--watch', function() {
137137
});
138138
});
139139

140+
it('reruns when "rs\\n" typed', function() {
141+
const testFile = path.join(this.tempDir, 'test.js');
142+
copyFixture('__default__', testFile);
143+
144+
return runMochaWatch([testFile], this.tempDir, mochaProcess => {
145+
mochaProcess.stdin.write('rs\n');
146+
}).then(results => {
147+
expect(results, 'to have length', 2);
148+
});
149+
});
150+
140151
it('reruns test when file starting with . and matching --extension is changed', function() {
141152
const testFile = path.join(this.tempDir, 'test.js');
142153
copyFixture('__default__', testFile);
@@ -282,11 +293,11 @@ describe('--watch', function() {
282293
function runMochaWatch(args, cwd, change) {
283294
const [mochaProcess, resultPromise] = helpers.invokeMochaAsync(
284295
[...args, '--watch', '--reporter', 'json'],
285-
{cwd}
296+
{cwd, stdio: 'pipe'}
286297
);
287298

288299
return sleep(1000)
289-
.then(() => change())
300+
.then(() => change(mochaProcess))
290301
.then(() => sleep(1000))
291302
.then(() => {
292303
mochaProcess.kill('SIGINT');

0 commit comments

Comments
 (0)