Skip to content

Commit

Permalink
Type "rs\n" to restart tests, fixes #871 (#3979)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
broofa authored and craigtaub committed Dec 13, 2019
1 parent 03b58f2 commit aab8555
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file|directory|glob>`

> _New in v7.0.0_
Expand Down
11 changes: 11 additions & 0 deletions lib/cli/watch-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
};

/**
Expand Down
15 changes: 13 additions & 2 deletions test/integration/options/watch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit aab8555

Please sign in to comment.