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
@@ -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

@@ -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 => {
This conversation was marked as resolved by doniyor2109

This comment has been minimized.

Copy link
@jeysal

jeysal Feb 14, 2019

Collaborator

Did you take a look at the json function in the runJest e2e test util file? That might be an easier way to count the number of tests run (we do it in many e2e tests already) unless I'm missing a reason why it needs to be done this way here :)

This comment has been minimized.

Copy link
@SimenB

SimenB Feb 14, 2019

Collaborator

That's a good point! Then we can assert the number of skipped tests as well

const {rest} = extractSortedSummary(str);
return rest.split('\n').filter(line => /[✓✕]/.test(line)).length;
};
@@ -0,0 +1,15 @@
/**
* 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';

it('runs only "it.only" tests', () => {
const {stderr} = runJest('focused-tests');
expect(countExecutedTests(stderr)).toBe(1);
});
@@ -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 */
This conversation was marked as resolved by doniyor2109

This comment has been minimized.

Copy link
@SimenB

SimenB Feb 14, 2019

Collaborator
Suggested change
/* eslint jest/no-focused-tests: 0 */
/* 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);
});
});
@@ -0,0 +1 @@
{}
@@ -55,6 +55,23 @@ 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 => {
if (!test.mode && shouldInheritMode) {
This conversation was marked as resolved by doniyor2109

This comment has been minimized.

Copy link
@jeysal

jeysal Feb 14, 2019

Collaborator

can wrap the whole statement in if(shouldInheritMode) to avoid looping at all if it's falsy

test.mode = currentDescribeBlock.mode;
}
});

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

if (currentDescribeBlock.parent) {
state.currentDescribeBlock = currentDescribeBlock.parent;
}
@@ -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,
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.