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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions e2e/__tests__/focusedTests.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
25 changes: 25 additions & 0 deletions e2e/focused-tests/__tests__/tests.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
1 change: 1 addition & 0 deletions e2e/focused-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
22 changes: 22 additions & 0 deletions packages/jest-circus/src/eventHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 1 addition & 7 deletions packages/jest-circus/src/utils.js
Original file line number Diff line number Diff line change
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