From ef3daa1cd220c242c55d6983fe7308c3e5851a91 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Demonte Date: Wed, 23 May 2018 15:04:03 +0200 Subject: [PATCH] Fix runs beforeAll/afterAll in excluded suites (#4820, #6166) --- CHANGELOG.md | 1 + .../__snapshots__/before-all-filtered.js.snap | 19 ++++++++++ e2e/__tests__/before-all-filtered.js | 18 ++++++++++ .../__tests__/before-all-filtered.test.js | 35 +++++++++++++++++++ e2e/before-all-filtered/package.json | 5 +++ packages/jest-circus/src/utils.js | 30 +++++++++++----- packages/jest-jasmine2/src/tree_processor.js | 15 ++++++++ 7 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 e2e/__tests__/__snapshots__/before-all-filtered.js.snap create mode 100644 e2e/__tests__/before-all-filtered.js create mode 100644 e2e/before-all-filtered/__tests__/before-all-filtered.test.js create mode 100644 e2e/before-all-filtered/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 430f82f530d5..aafb8aba8209 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * `[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)) ### 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 1c6618a1711b..90a5a31bf483 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); }