diff --git a/CHANGELOG.md b/CHANGELOG.md index 47d9f4a32957..811f26e02d9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * `[jest-each]` Make sure invalid arguments to `each` points back to the user's code ([#6347](https://github.com/facebook/jest/pull/6347)) * `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313)) * `[jest-cli]` Normalize slashes in paths in CLI output on Windows ([#6310](https://github.com/facebook/jest/pull/6310)) +* `[jest-cli]` Fix run beforeAll in excluded suites tests" mode. ([#6234](https://github.com/facebook/jest/pull/6234)) * `[jest-haste-map`] Compute SHA-1s for non-tracked files when using Node crawler ([#6264](https://github.com/facebook/jest/pull/6264)) ### Chore & Maintenance diff --git a/e2e/__tests__/__snapshots__/before-all-filtered.js.snap b/e2e/__tests__/__snapshots__/before-all-filtered.js.snap new file mode 100644 index 000000000000..ec312474433f --- /dev/null +++ b/e2e/__tests__/__snapshots__/before-all-filtered.js.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Correct BeforeAll run ensures the BeforeAll of ignored suite is not run 1`] = ` +" console.log __tests__/before-all-filtered.test.js:3 + beforeAll 1 + + console.log __tests__/before-all-filtered.test.js:6 + beforeEach 1 + + console.log __tests__/before-all-filtered.test.js:15 + It Foo + + console.log __tests__/before-all-filtered.test.js:9 + afterEach 1 + + console.log __tests__/before-all-filtered.test.js:12 + afterAll 1 +" +`; diff --git a/e2e/__tests__/before-all-filtered.js b/e2e/__tests__/before-all-filtered.js new file mode 100644 index 000000000000..55c2401b45da --- /dev/null +++ b/e2e/__tests__/before-all-filtered.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. 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 + */ +'use strict'; + +const runJest = require('../runJest'); + +describe('Correct BeforeAll run', () => { + it('ensures the BeforeAll of ignored suite is not run', () => { + const result = runJest('before-all-filtered'); + expect(result.stdout.replace(/\\/g, '/')).toMatchSnapshot(); + }); +}); diff --git a/e2e/before-all-filtered/__tests__/before-all-filtered.test.js b/e2e/before-all-filtered/__tests__/before-all-filtered.test.js new file mode 100644 index 000000000000..421560e4d12a --- /dev/null +++ b/e2e/before-all-filtered/__tests__/before-all-filtered.test.js @@ -0,0 +1,35 @@ +describe('test_1', () => { + beforeAll(() => { + console.log('beforeAll 1'); + }); + beforeEach(() => { + console.log('beforeEach 1'); + }); + afterEach(() => { + console.log('afterEach 1'); + }); + afterAll(() => { + console.log('afterAll 1'); + }); + it('foo', () => { + console.log('It Foo'); + }); +}); + +describe('test_2', () => { + beforeAll(() => { + console.log('beforeAll 2'); + }); + beforeEach(() => { + console.log('beforeEach 2'); + }); + afterEach(() => { + console.log('afterEach 2'); + }); + afterAll(() => { + console.log('afterAll 2'); + }); + it('foo', () => { + console.log('It Foo'); + }); +}); diff --git a/e2e/before-all-filtered/package.json b/e2e/before-all-filtered/package.json new file mode 100644 index 000000000000..0e2752593d25 --- /dev/null +++ b/e2e/before-all-filtered/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testNamePattern": "test_1" + } +} diff --git a/packages/jest-circus/src/utils.js b/packages/jest-circus/src/utils.js index 7d3dd4c7213b..00af896981dd 100644 --- a/packages/jest-circus/src/utils.js +++ b/packages/jest-circus/src/utils.js @@ -89,19 +89,33 @@ export const makeTest = ( }; }; +const hasEnabledTest = (describeBlock: DescribeBlock): boolean => { + const {hasFocusedTests, testNamePattern} = getState(); + return describeBlock.tests.some( + test => + !( + test.mode === 'skip' || + (hasFocusedTests && test.mode !== 'only') || + (testNamePattern && !testNamePattern.test(getTestID(test))) + ), + ); +}; + export const getAllHooksForDescribe = ( describe: DescribeBlock, ): {[key: 'beforeAll' | 'afterAll']: Array} => { const result = {afterAll: [], beforeAll: []}; - for (const hook of describe.hooks) { - switch (hook.type) { - case 'beforeAll': - result.beforeAll.push(hook); - break; - case 'afterAll': - result.afterAll.push(hook); - break; + if (hasEnabledTest(describe)) { + for (const hook of describe.hooks) { + switch (hook.type) { + case 'beforeAll': + result.beforeAll.push(hook); + break; + case 'afterAll': + result.afterAll.push(hook); + break; + } } } diff --git a/packages/jest-jasmine2/src/tree_processor.js b/packages/jest-jasmine2/src/tree_processor.js index 0ebcc01a59da..eed3eaa41a34 100644 --- a/packages/jest-jasmine2/src/tree_processor.js +++ b/packages/jest-jasmine2/src/tree_processor.js @@ -18,6 +18,7 @@ type Options = { type TreeNode = { afterAllFns: Array, beforeAllFns: Array, + disabled?: boolean, execute: (onComplete: () => void, enabled: boolean) => void, id: string, onException: (error: Error) => void, @@ -69,6 +70,17 @@ export default function treeProcessor(options: Options) { }; } + function hasEnabledTest(node: TreeNode) { + if (node.children) { + if (node.children.some(hasEnabledTest)) { + return true; + } + } else { + return !node.disabled; + } + return false; + } + function wrapChildren(node: TreeNode, enabled: boolean) { if (!node.children) { throw new Error('`node.children` is not defined.'); @@ -76,6 +88,9 @@ export default function treeProcessor(options: Options) { const children = node.children.map(child => ({ fn: getNodeHandler(child, enabled), })); + if (!hasEnabledTest(node)) { + return []; + } return node.beforeAllFns.concat(children).concat(node.afterAllFns); }