Skip to content

Commit 696d424

Browse files
doniyor2109SimenB
authored andcommitted
Inherit "only" mode unless there is tests with "only" mode already (#7888)
1 parent 0c1d5f9 commit 696d424

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- `[expect]` Fix custom async matcher stack trace ([#7652](https://github.com/facebook/jest/pull/7652))
1313
- `[jest-changed-files]` Improve default file selection for Mercurial repos ([#7880](https://github.com/facebook/jest/pull/7880))
1414
- `[jest-validate]` Fix validating async functions ([#7894](https://github.com/facebook/jest/issues/7894))
15+
- `[jest-circus]` Fix bug with test.only ([#7888](https://github.com/facebook/jest/pull/7888))
1516

1617
### Chore & Maintenance
1718

e2e/__tests__/focusedTests.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
import {json} from '../runJest';
10+
11+
it('runs only "it.only" tests', () => {
12+
const {
13+
json: {numPassedTests, numPendingTests},
14+
} = json('focused-tests');
15+
expect(numPassedTests).toBe(1);
16+
expect(numPendingTests).toBe(2);
17+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
/* eslint-disable jest/no-focused-tests */
10+
11+
describe('describe', () => {
12+
it('it', () => {
13+
expect(1).toBe(1);
14+
});
15+
});
16+
17+
describe.only('describe only', () => {
18+
it.only('it only', () => {
19+
expect(1).toBe(1);
20+
});
21+
22+
it('it', () => {
23+
expect(1).toBe(1);
24+
});
25+
});

e2e/focused-tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

packages/jest-circus/src/eventHandler.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ const eventHandler: EventHandler = (event, state): void => {
5555
});
5656
}
5757

58+
// inherit mode from its parent describe but
59+
// do not inherit "only" mode when there is already tests with "only" mode
60+
const shouldInheritMode = !(
61+
currentDescribeBlock.mode === 'only' &&
62+
currentDescribeBlock.tests.find(test => test.mode === 'only')
63+
);
64+
65+
if (shouldInheritMode) {
66+
currentDescribeBlock.tests.forEach(test => {
67+
if (!test.mode) {
68+
test.mode = currentDescribeBlock.mode;
69+
}
70+
});
71+
}
72+
73+
if (
74+
!state.hasFocusedTests &&
75+
currentDescribeBlock.tests.some(test => test.mode === 'only')
76+
) {
77+
state.hasFocusedTests = true;
78+
}
79+
5880
if (currentDescribeBlock.parent) {
5981
state.currentDescribeBlock = currentDescribeBlock.parent;
6082
}

packages/jest-circus/src/utils.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ export const makeTest = (
6565
timeout: ?number,
6666
asyncError: Exception,
6767
): TestEntry => {
68-
let _mode = mode;
69-
if (!mode) {
70-
// if not set explicitly, inherit from its parent describe
71-
_mode = parent.mode;
72-
}
73-
7468
const errors: Array<[?Exception, Exception]> = [];
7569

7670
return {
@@ -79,7 +73,7 @@ export const makeTest = (
7973
errors,
8074
fn,
8175
invocations: 0,
82-
mode: _mode,
76+
mode,
8377
name: convertDescriptorToString(name),
8478
parent,
8579
startedAt: null,

0 commit comments

Comments
 (0)