Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherit "only" mode unless there is tests with "only" mode already #7888

Merged
merged 12 commits into from Feb 14, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,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-changed-files]` Improve default file selection for Mercurial repos ([#7880](https://github.com/facebook/jest/pull/7880))
- `[jest-circus]` Fix bug with test.only ([#7888](https://github.com/facebook/jest/pull/7888))

### Chore & Maintenance

Expand Down
5 changes: 5 additions & 0 deletions e2e/Utils.js
Expand Up @@ -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 => {
doniyor2109 marked this conversation as resolved.
Show resolved Hide resolved
const {rest} = extractSortedSummary(str);
return rest.split('\n').filter(line => /[✓✕]/.test(line)).length;
};
16 changes: 16 additions & 0 deletions 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');
doniyor2109 marked this conversation as resolved.
Show resolved Hide resolved
const {stderr} = runJest('jest-circus', [`--testRunner=${jestCircusPath}`]);
expect(countExecutedTests(stderr)).toBe(1);
});
25 changes: 25 additions & 0 deletions e2e/jest-circus/__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 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);
});
});
1 change: 1 addition & 0 deletions e2e/jest-circus/package.json
@@ -0,0 +1 @@
{}
18 changes: 18 additions & 0 deletions packages/jest-circus/src/eventHandler.js
Expand Up @@ -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),
doniyor2109 marked this conversation as resolved.
Show resolved Hide resolved
);

if (currentDescribeBlock.tests.some(test => test.mode === 'only')) {
state.hasFocusedTests = true;
}

if (currentDescribeBlock.parent) {
state.currentDescribeBlock = currentDescribeBlock.parent;
}
Expand Down
8 changes: 1 addition & 7 deletions packages/jest-circus/src/utils.js
Expand Up @@ -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 {
Expand All @@ -79,7 +73,7 @@ export const makeTest = (
errors,
fn,
invocations: 0,
mode: _mode,
mode,
name: convertDescriptorToString(name),
parent,
startedAt: null,
Expand Down