From b10337697e804850da8b59b51c7da5642527ffcc Mon Sep 17 00:00:00 2001
From: Dan Abramov <dan.abramov@gmail.com>
Date: Mon, 1 Oct 2018 22:08:14 +0100
Subject: [PATCH] Don't crash npm test when hg/git are missing (#5212)

---
 packages/react-scripts/scripts/test.js | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/packages/react-scripts/scripts/test.js b/packages/react-scripts/scripts/test.js
index 0606549fb..731924f4a 100644
--- a/packages/react-scripts/scripts/test.js
+++ b/packages/react-scripts/scripts/test.js
@@ -31,15 +31,36 @@ if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
 // @remove-on-eject-end
 
 const jest = require('jest');
+const execSync = require('child_process').execSync;
 let argv = process.argv.slice(2);
 
+function isInGitRepository() {
+  try {
+    execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
+    return true;
+  } catch (e) {
+    return false;
+  }
+}
+
+function isInMercurialRepository() {
+  try {
+    execSync('hg --cwd . root', { stdio: 'ignore' });
+    return true;
+  } catch (e) {
+    return false;
+  }
+}
+
 // Watch unless on CI, in coverage mode, or explicitly running all tests
 if (
   !process.env.CI &&
   argv.indexOf('--coverage') === -1 &&
   argv.indexOf('--watchAll') === -1
 ) {
-  argv.push('--watch');
+  // https://github.com/facebook/create-react-app/issues/5210
+  const hasSourceControl = isInGitRepository() || isInMercurialRepository();
+  argv.push(hasSourceControl ? '--watch' : '--watchAll');
 }
 
 // @remove-on-eject-begin
-- 
GitLab