Repository: remy/nodemon
Pull request #1151 (2017-12-06 21:18:31)

test: fork child node processes
it blends


Patch:
diff --git a/.travis.yml b/.travis.yml
index 4557e55..b12423b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -9,7 +9,7 @@ node_js:
   - '8'
   - '6'
 before_install:
-  - if [ "$TRAVIS_BRANCH" == "master" ]; then echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" > .npmrc; fi
+  - if [ "$TRAVIS_BRANCH" == "master" ]; then export _authToken=$NPM_TOKEN; fi
 after_success:
   - npm run semantic-release
 branches:
diff --git a/lib/monitor/run.js b/lib/monitor/run.js
index 2baebff..48b898f 100644
--- a/lib/monitor/run.js
+++ b/lib/monitor/run.js
@@ -4,6 +4,7 @@ var bus = utils.bus;
 var childProcess = require('child_process');
 var spawn = childProcess.spawn;
 var exec = childProcess.exec;
+var fork = childProcess.fork;
 var watch = require('./watch').watch;
 var config = require('../config');
 var child = null; // the actual child process we spawn
@@ -82,7 +83,17 @@ function run(options) {
     });
   }
 
-  child = spawn.apply(null, spawnArgs);
+  if (executable === 'node') {
+    var forkArgs = cmd.args.slice(1);
+    var env = utils.merge(options.execOptions.env, process.env);
+    stdio.push('ipc');
+    child = fork(options.execOptions.script, forkArgs, {
+      env: env,
+      stdio: stdio,
+    });
+  } else {
+    child = spawn.apply(null, spawnArgs);
+  }
 
   if (config.required) {
     var emit = {
diff --git a/lib/monitor/watch.js b/lib/monitor/watch.js
index 1a8f091..68220b5 100644
--- a/lib/monitor/watch.js
+++ b/lib/monitor/watch.js
@@ -79,7 +79,8 @@ function watch() {
         }
 
         watchedFiles.push(file);
-        total++;
+        watchedFiles = Array.from(new Set(watchedFiles)); // ensure no dupes
+        total = watchedFiles.length;
         debug('watching dir: %s', file);
       });
       watcher.on('ready', function () {


Test patch:
diff --git a/test/monitor/run.test.js b/test/monitor/run.test.js
index 2714954..572f63a 100644
--- a/test/monitor/run.test.js
+++ b/test/monitor/run.test.js
@@ -153,32 +153,29 @@ describe('when nodemon runs (2)', function () {
       }, 1500);
     });
 
-  // it('should kill child on SIGINT', function (done) {
-  //   fs.writeFileSync(tmp, 'setTimeout(function () { var n = 10; }, 10000)');
-
-  //   nodemon({ script: tmp, verbose: true }).on('start', function () {
-  //     assert(true, 'nodemon is waiting for a change');
-
-  //     setTimeout(function () {
-  //       // process.once('SIGINT', function () {
-  //       //   // do nothing
-  //       //   console.log('not going to exit');
-  //       // });
-
-  //       process.kill(process.pid, 'SIGINT');
-  //     }, 1000);
-  //   }).on('crash', function () {
-  //     assert(false, 'detected crashed state');
-  //   }).on('exit', function () {
-  //     assert(true, 'quit correctly');
-  //     nodemon.reset();
-  //     done();
-
-  //     setTimeout(function () {
-  //       process.kill(process.pid, 'SIGINT');
-  //     }, 1000);
-
-  //   });
-
-  // });
+  it('should kill child on SIGINT', function (done) {
+    fs.writeFileSync(tmp, 'setTimeout(function () { var n = 10; }, 10000)');
+
+    nodemon({ script: tmp, verbose: true }).on('start', function () {
+      assert(true, 'nodemon is waiting for a change');
+
+      setTimeout(function () {
+        process.once('SIGINT', function () {
+          // do nothing
+        });
+
+        process.kill(process.pid, 'SIGINT');
+      }, 1000);
+    }).on('crash', function () {
+      assert(false, 'detected crashed state');
+    }).on('exit', function () {
+      assert(true, 'quit correctly');
+      nodemon.reset();
+      done();
+
+      setTimeout(function () {
+        process.kill(process.pid, 'SIGINT');
+      }, 1000);
+    });
+  });
 });