From 7d8fba3b48a23ff6e4254a532c4807818b04646d Mon Sep 17 00:00:00 2001
From: Rafael Oleza <rafeca@gmail.com>
Date: Fri, 13 Jul 2018 13:29:40 +0200
Subject: [PATCH 1/4] Tweak jest-haste-map watchman initial query to make it
 faster

---
 CHANGELOG.md                                     |  4 ++++
 packages/jest-haste-map/src/crawlers/watchman.js | 12 ++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7e147b7289c6..21eac2ccd6f2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,10 @@
 
 - `[jest-cli]` Watch plugins now have access to a broader range of global configuration options in their `updateConfigAndRun` callbacks, so they can provide a wider set of extra features ([#6473](https://github.com/facebook/jest/pull/6473))
 
+## Fixes
+
+- `[jest-haste-map]` Optimize watchman crawler by using `glob` on initial query
+
 ## 23.4.0
 
 ### Features
diff --git a/packages/jest-haste-map/src/crawlers/watchman.js b/packages/jest-haste-map/src/crawlers/watchman.js
index 9da058486f78..6adb9802877f 100644
--- a/packages/jest-haste-map/src/crawlers/watchman.js
+++ b/packages/jest-haste-map/src/crawlers/watchman.js
@@ -93,6 +93,8 @@ module.exports = async function watchmanCrawl(
       Array.from(rootProjectDirMappings).map(
         async ([root, directoryFilters]) => {
           const expression = Array.from(defaultWatchExpression);
+          const glob = [];
+
           if (directoryFilters.length > 0) {
             expression.push([
               'anyof',
@@ -100,11 +102,17 @@ module.exports = async function watchmanCrawl(
             ]);
           }
 
+          for (const directory of directoryFilters) {
+            for (const extension of extensions) {
+              glob.push(`${directory}/**/*.${extension}`);
+            }
+          }
+
           const query = clocks[root]
             ? // Use the `since` generator if we have a clock available
               {expression, fields, since: clocks[root]}
-            : // Otherwise use the `suffix` generator
-              {expression, fields, suffix: extensions};
+            : // Otherwise use the `glob` filter
+              {expression, fields, glob};
 
           const response = await cmd('query', root, query);
 

From 6a3e875139f9fa4ccd34a1f612099e61698881d8 Mon Sep 17 00:00:00 2001
From: Rafael de Oleza <rafeca@gmail.com>
Date: Fri, 13 Jul 2018 13:41:09 +0200
Subject: [PATCH 2/4] Update CHANGELOG.md

---
 CHANGELOG.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 21eac2ccd6f2..14c2f2a201b0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,7 +6,7 @@
 
 ## Fixes
 
-- `[jest-haste-map]` Optimize watchman crawler by using `glob` on initial query
+- `[jest-haste-map]` Optimize watchman crawler by using `glob` on initial query ([#6689](https://github.com/facebook/jest/pull/6689))
 
 ## 23.4.0
 

From 9ac7bc380687aa17311a50dc77db6a993373196a Mon Sep 17 00:00:00 2001
From: Rafael Oleza <rafeca@gmail.com>
Date: Fri, 13 Jul 2018 14:16:58 +0200
Subject: [PATCH 3/4] Fix watchhman crawler unit tests

---
 .../src/crawlers/__tests__/watchman.test.js          | 12 ++++++++++--
 packages/jest-haste-map/src/crawlers/watchman.js     | 10 +++++++---
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js b/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js
index 36c190cce31c..a0c37f493180 100644
--- a/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js
+++ b/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js
@@ -141,7 +141,12 @@ describe('watchman watch', () => {
 
       expect(query[2].fields).toEqual(['name', 'exists', 'mtime_ms']);
 
-      expect(query[2].suffix).toEqual(['js', 'json']);
+      expect(query[2].glob).toEqual([
+        'fruits/**/*.js',
+        'fruits/**/*.json',
+        'vegetables/**/*.js',
+        'vegetables/**/*.json',
+      ]);
 
       expect(data.clocks).toEqual({
         [ROOT_MOCK]: 'c:fake-clock:1',
@@ -412,7 +417,10 @@ describe('watchman watch', () => {
 
       expect(query[2].fields).toEqual(['name', 'exists', 'mtime_ms']);
 
-      expect(query[2].suffix).toEqual(['js', 'json']);
+      expect(query[2].glob).toEqual([
+        '**/*.js',
+        '**/*.json',
+      ]);
 
       expect(data.clocks).toEqual({
         [ROOT_MOCK]: 'c:fake-clock:1',
diff --git a/packages/jest-haste-map/src/crawlers/watchman.js b/packages/jest-haste-map/src/crawlers/watchman.js
index 6adb9802877f..9e6af68d9a71 100644
--- a/packages/jest-haste-map/src/crawlers/watchman.js
+++ b/packages/jest-haste-map/src/crawlers/watchman.js
@@ -100,11 +100,15 @@ module.exports = async function watchmanCrawl(
               'anyof',
               ...directoryFilters.map(dir => ['dirname', dir]),
             ]);
-          }
 
-          for (const directory of directoryFilters) {
+            for (const directory of directoryFilters) {
+              for (const extension of extensions) {
+                glob.push(`${directory}/**/*.${extension}`);
+              }
+            }
+          } else {
             for (const extension of extensions) {
-              glob.push(`${directory}/**/*.${extension}`);
+              glob.push(`**/*.${extension}`);
             }
           }
 

From 1fe87e5edb42aff09d252d355f5c503f2b7dc967 Mon Sep 17 00:00:00 2001
From: Rafael Oleza <rafeca@gmail.com>
Date: Fri, 13 Jul 2018 14:40:25 +0200
Subject: [PATCH 4/4] Fix linter warnings

---
 .../jest-haste-map/src/crawlers/__tests__/watchman.test.js   | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js b/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js
index a0c37f493180..0db22b9abd21 100644
--- a/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js
+++ b/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js
@@ -417,10 +417,7 @@ describe('watchman watch', () => {
 
       expect(query[2].fields).toEqual(['name', 'exists', 'mtime_ms']);
 
-      expect(query[2].glob).toEqual([
-        '**/*.js',
-        '**/*.json',
-      ]);
+      expect(query[2].glob).toEqual(['**/*.js', '**/*.json']);
 
       expect(data.clocks).toEqual({
         [ROOT_MOCK]: 'c:fake-clock:1',