Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix runs beforeAll/afterAll in excluded suites (#4820, #6166)
  • Loading branch information
jbdemonte committed May 29, 2018
commit ef3daa1cd220c242c55d6983fe7308c3e5851a91
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions e2e/__tests__/__snapshots__/before-all-filtered.js.snap
Original file line number Diff line number Diff line change
@@ -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
"
`;
18 changes: 18 additions & 0 deletions e2e/__tests__/before-all-filtered.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
35 changes: 35 additions & 0 deletions e2e/before-all-filtered/__tests__/before-all-filtered.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
5 changes: 5 additions & 0 deletions e2e/before-all-filtered/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testNamePattern": "test_1"
}
}
30 changes: 22 additions & 8 deletions packages/jest-circus/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hook>} => {
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;
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions packages/jest-jasmine2/src/tree_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Options = {
type TreeNode = {
afterAllFns: Array<any>,
beforeAllFns: Array<any>,
disabled?: boolean,
execute: (onComplete: () => void, enabled: boolean) => void,
id: string,
onException: (error: Error) => void,
Expand Down Expand Up @@ -69,13 +70,27 @@ 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.');
}
const children = node.children.map(child => ({
fn: getNodeHandler(child, enabled),
}));
if (!hasEnabledTest(node)) {
return [];
}
return node.beforeAllFns.concat(children).concat(node.afterAllFns);
}

Expand Down