From 696d424ecb7753778dc54c58c58d824f9055ed9c Mon Sep 17 00:00:00 2001
From: doniyor2109 <doniyor2109@gmail.com>
Date: Thu, 14 Feb 2019 22:32:47 +0500
Subject: [PATCH] Inherit "only" mode unless there is tests with "only" mode
 already (#7888)

---
 CHANGELOG.md                             |  1 +
 e2e/__tests__/focusedTests.test.js       | 17 ++++++++++++++++
 e2e/focused-tests/__tests__/tests.js     | 25 ++++++++++++++++++++++++
 e2e/focused-tests/package.json           |  1 +
 packages/jest-circus/src/eventHandler.js | 22 +++++++++++++++++++++
 packages/jest-circus/src/utils.js        |  8 +-------
 6 files changed, 67 insertions(+), 7 deletions(-)
 create mode 100644 e2e/__tests__/focusedTests.test.js
 create mode 100644 e2e/focused-tests/__tests__/tests.js
 create mode 100644 e2e/focused-tests/package.json

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 909f7ed9490e..3816ad097265 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@
 - `[expect]` Fix custom async matcher stack trace ([#7652](https://github.com/facebook/jest/pull/7652))
 - `[jest-changed-files]` Improve default file selection for Mercurial repos ([#7880](https://github.com/facebook/jest/pull/7880))
 - `[jest-validate]` Fix validating async functions ([#7894](https://github.com/facebook/jest/issues/7894))
+- `[jest-circus]` Fix bug with test.only ([#7888](https://github.com/facebook/jest/pull/7888))
 
 ### Chore & Maintenance
 
diff --git a/e2e/__tests__/focusedTests.test.js b/e2e/__tests__/focusedTests.test.js
new file mode 100644
index 000000000000..e0fe7493b78b
--- /dev/null
+++ b/e2e/__tests__/focusedTests.test.js
@@ -0,0 +1,17 @@
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ *
+ * @flow
+ */
+import {json} from '../runJest';
+
+it('runs only "it.only" tests', () => {
+  const {
+    json: {numPassedTests, numPendingTests},
+  } = json('focused-tests');
+  expect(numPassedTests).toBe(1);
+  expect(numPendingTests).toBe(2);
+});
diff --git a/e2e/focused-tests/__tests__/tests.js b/e2e/focused-tests/__tests__/tests.js
new file mode 100644
index 000000000000..2b2bde1d831d
--- /dev/null
+++ b/e2e/focused-tests/__tests__/tests.js
@@ -0,0 +1,25 @@
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+'use strict';
+
+/* eslint-disable jest/no-focused-tests */
+
+describe('describe', () => {
+  it('it', () => {
+    expect(1).toBe(1);
+  });
+});
+
+describe.only('describe only', () => {
+  it.only('it only', () => {
+    expect(1).toBe(1);
+  });
+
+  it('it', () => {
+    expect(1).toBe(1);
+  });
+});
diff --git a/e2e/focused-tests/package.json b/e2e/focused-tests/package.json
new file mode 100644
index 000000000000..0967ef424bce
--- /dev/null
+++ b/e2e/focused-tests/package.json
@@ -0,0 +1 @@
+{}
diff --git a/packages/jest-circus/src/eventHandler.js b/packages/jest-circus/src/eventHandler.js
index f1b64054f693..663249c689a8 100644
--- a/packages/jest-circus/src/eventHandler.js
+++ b/packages/jest-circus/src/eventHandler.js
@@ -55,6 +55,28 @@ const eventHandler: EventHandler = (event, state): void => {
         });
       }
 
+      // inherit mode from its parent describe but
+      // do not inherit "only" mode when there is already tests with "only" mode
+      const shouldInheritMode = !(
+        currentDescribeBlock.mode === 'only' &&
+        currentDescribeBlock.tests.find(test => test.mode === 'only')
+      );
+
+      if (shouldInheritMode) {
+        currentDescribeBlock.tests.forEach(test => {
+          if (!test.mode) {
+            test.mode = currentDescribeBlock.mode;
+          }
+        });
+      }
+
+      if (
+        !state.hasFocusedTests &&
+        currentDescribeBlock.tests.some(test => test.mode === 'only')
+      ) {
+        state.hasFocusedTests = true;
+      }
+
       if (currentDescribeBlock.parent) {
         state.currentDescribeBlock = currentDescribeBlock.parent;
       }
diff --git a/packages/jest-circus/src/utils.js b/packages/jest-circus/src/utils.js
index 80a58fc2d9cd..0c56d8619b5b 100644
--- a/packages/jest-circus/src/utils.js
+++ b/packages/jest-circus/src/utils.js
@@ -65,12 +65,6 @@ export const makeTest = (
   timeout: ?number,
   asyncError: Exception,
 ): TestEntry => {
-  let _mode = mode;
-  if (!mode) {
-    // if not set explicitly, inherit from its parent describe
-    _mode = parent.mode;
-  }
-
   const errors: Array<[?Exception, Exception]> = [];
 
   return {
@@ -79,7 +73,7 @@ export const makeTest = (
     errors,
     fn,
     invocations: 0,
-    mode: _mode,
+    mode,
     name: convertDescriptorToString(name),
     parent,
     startedAt: null,