From cb9e204d921c0649a76a1138a18036801c5efe64 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 12:10:38 +0500 Subject: [PATCH 01/10] Inherit "only" mode unless there is tests with "only" mode already --- e2e/Utils.js | 5 +++++ e2e/__tests__/jestCircus.test.js | 16 ++++++++++++++++ e2e/jest-circus/__tests__/tests.js | 17 +++++++++++++++++ e2e/jest-circus/package.json | 1 + packages/jest-circus/src/eventHandler.js | 18 ++++++++++++++++++ packages/jest-circus/src/utils.js | 8 +------- 6 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 e2e/__tests__/jestCircus.test.js create mode 100644 e2e/jest-circus/__tests__/tests.js create mode 100644 e2e/jest-circus/package.json diff --git a/e2e/Utils.js b/e2e/Utils.js index 57e80a2fcd0f..b1e2841e8d32 100644 --- a/e2e/Utils.js +++ b/e2e/Utils.js @@ -225,3 +225,8 @@ export const normalizeIcons = (str: string) => { .replace(new RegExp('\u00D7', 'g'), '\u2715') .replace(new RegExp('\u221A', 'g'), '\u2713'); }; + +export const countExecutedTests = (str: string): number => { + const {rest} = extractSortedSummary(str); + return rest.split('\n').filter(line => /[✓✕]/.test(line)).length; +}; diff --git a/e2e/__tests__/jestCircus.test.js b/e2e/__tests__/jestCircus.test.js new file mode 100644 index 000000000000..39beed922c91 --- /dev/null +++ b/e2e/__tests__/jestCircus.test.js @@ -0,0 +1,16 @@ +/** + * 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 runJest from '../runJest'; +import {countExecutedTests} from '../Utils'; + +test('run only "it.only" tests', () => { + const jestCircusPath = require.resolve('../../packages/jest-circus/runner'); + const {stderr} = runJest('jest-circus', [`--testRunner=${jestCircusPath}`]); + expect(countExecutedTests(stderr)).toBe(1); +}); diff --git a/e2e/jest-circus/__tests__/tests.js b/e2e/jest-circus/__tests__/tests.js new file mode 100644 index 000000000000..8cf333eb6d1f --- /dev/null +++ b/e2e/jest-circus/__tests__/tests.js @@ -0,0 +1,17 @@ +/* eslint jest/no-focused-tests: 0 */ + +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/jest-circus/package.json b/e2e/jest-circus/package.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/e2e/jest-circus/package.json @@ -0,0 +1 @@ +{} diff --git a/packages/jest-circus/src/eventHandler.js b/packages/jest-circus/src/eventHandler.js index f1b64054f693..9afcfd585863 100644 --- a/packages/jest-circus/src/eventHandler.js +++ b/packages/jest-circus/src/eventHandler.js @@ -55,6 +55,24 @@ 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') + ); + + currentDescribeBlock.tests.forEach( + test => + !test.mode && + shouldInheritMode && + (test.mode = currentDescribeBlock.mode), + ); + + if (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, From cf97e4e139529f476608939ea889d8724c383f85 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 12:17:22 +0500 Subject: [PATCH 02/10] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e24ec6d84c6..1b79441302e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `[jest-cli]` Refactor `-o` and `--coverage` combined ([#7611](https://github.com/facebook/jest/pull/7611)) - `[expect]` Fix custom async matcher stack trace ([#7652](https://github.com/facebook/jest/pull/7652)) +- `[jest-circus]` Fix bug with test.only ([#7888](https://github.com/facebook/jest/pull/7888)) ### Chore & Maintenance From 942910303f6117e5c662e414e6e7c67e8b38013e Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 12:25:59 +0500 Subject: [PATCH 03/10] Added license header --- e2e/jest-circus/__tests__/tests.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/e2e/jest-circus/__tests__/tests.js b/e2e/jest-circus/__tests__/tests.js index 8cf333eb6d1f..16aeb996c49b 100644 --- a/e2e/jest-circus/__tests__/tests.js +++ b/e2e/jest-circus/__tests__/tests.js @@ -1,3 +1,11 @@ +/** + * 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 jest/no-focused-tests: 0 */ describe('describe', () => { From d0a6c2dbfd9b80d343a4bf8ac855d9f3e9cb3930 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 20:37:40 +0500 Subject: [PATCH 04/10] Remove runner options --- e2e/__tests__/jestCircus.test.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/e2e/__tests__/jestCircus.test.js b/e2e/__tests__/jestCircus.test.js index 39beed922c91..e7c26947c7a3 100644 --- a/e2e/__tests__/jestCircus.test.js +++ b/e2e/__tests__/jestCircus.test.js @@ -9,8 +9,7 @@ import runJest from '../runJest'; import {countExecutedTests} from '../Utils'; -test('run only "it.only" tests', () => { - const jestCircusPath = require.resolve('../../packages/jest-circus/runner'); - const {stderr} = runJest('jest-circus', [`--testRunner=${jestCircusPath}`]); +it('runs only "it.only" tests', () => { + const {stderr} = runJest('jest-circus'); expect(countExecutedTests(stderr)).toBe(1); }); From f11e96a2931a1c134c1e071cdcf14b7cf71eca74 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 20:38:55 +0500 Subject: [PATCH 05/10] Rename tests --- e2e/__tests__/{jestCircus.test.js => focusedTests.test.js} | 0 e2e/{jest-circus => focused-tests}/__tests__/tests.js | 0 e2e/{jest-circus => focused-tests}/package.json | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename e2e/__tests__/{jestCircus.test.js => focusedTests.test.js} (100%) rename e2e/{jest-circus => focused-tests}/__tests__/tests.js (100%) rename e2e/{jest-circus => focused-tests}/package.json (100%) diff --git a/e2e/__tests__/jestCircus.test.js b/e2e/__tests__/focusedTests.test.js similarity index 100% rename from e2e/__tests__/jestCircus.test.js rename to e2e/__tests__/focusedTests.test.js diff --git a/e2e/jest-circus/__tests__/tests.js b/e2e/focused-tests/__tests__/tests.js similarity index 100% rename from e2e/jest-circus/__tests__/tests.js rename to e2e/focused-tests/__tests__/tests.js diff --git a/e2e/jest-circus/package.json b/e2e/focused-tests/package.json similarity index 100% rename from e2e/jest-circus/package.json rename to e2e/focused-tests/package.json From 22c7de11cff48eefc7d2b8dfbada67fa7317c8a1 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 20:40:06 +0500 Subject: [PATCH 06/10] Rename test name --- e2e/__tests__/focusedTests.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/__tests__/focusedTests.test.js b/e2e/__tests__/focusedTests.test.js index e7c26947c7a3..2d5c4216d190 100644 --- a/e2e/__tests__/focusedTests.test.js +++ b/e2e/__tests__/focusedTests.test.js @@ -10,6 +10,6 @@ import runJest from '../runJest'; import {countExecutedTests} from '../Utils'; it('runs only "it.only" tests', () => { - const {stderr} = runJest('jest-circus'); + const {stderr} = runJest('focused-tests'); expect(countExecutedTests(stderr)).toBe(1); }); From 34f93c3953a2951c13e24f7bcb53f3270211d021 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 20:42:26 +0500 Subject: [PATCH 07/10] Minor tweaks --- packages/jest-circus/src/eventHandler.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/jest-circus/src/eventHandler.js b/packages/jest-circus/src/eventHandler.js index 9afcfd585863..69fe607459cb 100644 --- a/packages/jest-circus/src/eventHandler.js +++ b/packages/jest-circus/src/eventHandler.js @@ -62,12 +62,11 @@ const eventHandler: EventHandler = (event, state): void => { currentDescribeBlock.tests.find(test => test.mode === 'only') ); - currentDescribeBlock.tests.forEach( - test => - !test.mode && - shouldInheritMode && - (test.mode = currentDescribeBlock.mode), - ); + currentDescribeBlock.tests.forEach(test => { + if (!test.mode && shouldInheritMode) { + test.mode = currentDescribeBlock.mode; + } + }); if (currentDescribeBlock.tests.some(test => test.mode === 'only')) { state.hasFocusedTests = true; From 4e2be4b034532434c583795185e5d379ec1cf8ef Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 21:53:47 +0500 Subject: [PATCH 08/10] Minor tweaks --- e2e/Utils.js | 5 ----- e2e/__tests__/focusedTests.test.js | 10 ++++++---- packages/jest-circus/src/eventHandler.js | 12 +++++++----- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/e2e/Utils.js b/e2e/Utils.js index b1e2841e8d32..57e80a2fcd0f 100644 --- a/e2e/Utils.js +++ b/e2e/Utils.js @@ -225,8 +225,3 @@ export const normalizeIcons = (str: string) => { .replace(new RegExp('\u00D7', 'g'), '\u2715') .replace(new RegExp('\u221A', 'g'), '\u2713'); }; - -export const countExecutedTests = (str: string): number => { - const {rest} = extractSortedSummary(str); - return rest.split('\n').filter(line => /[✓✕]/.test(line)).length; -}; diff --git a/e2e/__tests__/focusedTests.test.js b/e2e/__tests__/focusedTests.test.js index 2d5c4216d190..e0fe7493b78b 100644 --- a/e2e/__tests__/focusedTests.test.js +++ b/e2e/__tests__/focusedTests.test.js @@ -6,10 +6,12 @@ * * @flow */ -import runJest from '../runJest'; -import {countExecutedTests} from '../Utils'; +import {json} from '../runJest'; it('runs only "it.only" tests', () => { - const {stderr} = runJest('focused-tests'); - expect(countExecutedTests(stderr)).toBe(1); + const { + json: {numPassedTests, numPendingTests}, + } = json('focused-tests'); + expect(numPassedTests).toBe(1); + expect(numPendingTests).toBe(2); }); diff --git a/packages/jest-circus/src/eventHandler.js b/packages/jest-circus/src/eventHandler.js index 69fe607459cb..a1d29a9b1936 100644 --- a/packages/jest-circus/src/eventHandler.js +++ b/packages/jest-circus/src/eventHandler.js @@ -62,11 +62,13 @@ const eventHandler: EventHandler = (event, state): void => { currentDescribeBlock.tests.find(test => test.mode === 'only') ); - currentDescribeBlock.tests.forEach(test => { - if (!test.mode && shouldInheritMode) { - test.mode = currentDescribeBlock.mode; - } - }); + if (shouldInheritMode) { + currentDescribeBlock.tests.forEach(test => { + if (!test.mode) { + test.mode = currentDescribeBlock.mode; + } + }); + } if (currentDescribeBlock.tests.some(test => test.mode === 'only')) { state.hasFocusedTests = true; From 9b7a528c54eea0dca9991ce75c021fcb78b02282 Mon Sep 17 00:00:00 2001 From: doniyor2109 Date: Thu, 14 Feb 2019 21:59:16 +0500 Subject: [PATCH 09/10] Minor performance improvement --- packages/jest-circus/src/eventHandler.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/jest-circus/src/eventHandler.js b/packages/jest-circus/src/eventHandler.js index a1d29a9b1936..663249c689a8 100644 --- a/packages/jest-circus/src/eventHandler.js +++ b/packages/jest-circus/src/eventHandler.js @@ -70,7 +70,10 @@ const eventHandler: EventHandler = (event, state): void => { }); } - if (currentDescribeBlock.tests.some(test => test.mode === 'only')) { + if ( + !state.hasFocusedTests && + currentDescribeBlock.tests.some(test => test.mode === 'only') + ) { state.hasFocusedTests = true; } From 8dc66407bd6b69829571e88be5e00408a7b334e2 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 14 Feb 2019 22:03:55 +0500 Subject: [PATCH 10/10] Minor changes Co-Authored-By: doniyor2109 --- e2e/focused-tests/__tests__/tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/focused-tests/__tests__/tests.js b/e2e/focused-tests/__tests__/tests.js index 16aeb996c49b..2b2bde1d831d 100644 --- a/e2e/focused-tests/__tests__/tests.js +++ b/e2e/focused-tests/__tests__/tests.js @@ -6,7 +6,7 @@ */ 'use strict'; -/* eslint jest/no-focused-tests: 0 */ +/* eslint-disable jest/no-focused-tests */ describe('describe', () => { it('it', () => {