Skip to content

Commit

Permalink
fix: don't run beforeAll/afterAll in skipped block (#9931)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeiea authored May 1, 2020
1 parent 80b6905 commit 649796d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-jasmine2]` Don't run `beforeAll` / `afterAll` in skipped describe block ([#9931](https://github.com/facebook/jest/pull/9931))

### Chore & Maintenance

### Performance
Expand Down
19 changes: 19 additions & 0 deletions e2e/__tests__/skipBeforeAfterAll.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.
*/

import * as path from 'path';
import {json as runWithJson} from '../runJest';

const DIR = path.resolve(__dirname, '../before-all-skipped');

test('correctly skip `beforeAll`s in skipped tests', () => {
const {json} = runWithJson(DIR);

expect(json.numTotalTests).toBe(6);
expect(json.numPassedTests).toBe(4);
expect(json.numPendingTests).toBe(2);
});
58 changes: 58 additions & 0 deletions e2e/before-all-skipped/__tests__/beforeAllFiltered.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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.
*/

let hasBeforeAllRun = false;
let hasAfterAllRun = false;

describe.skip('in describe.skip', () => {
describe('in describe', () => {
beforeAll(() => {
hasBeforeAllRun = true;
});

afterAll(() => {
hasAfterAllRun = true;
});

test('it should be skipped', () => {
throw new Error('This should never happen');
});
});
});

test('describe.skip should not run beforeAll', () => {
expect(hasBeforeAllRun).toBe(false);
});

test('describe.skip should not run afterAll', () => {
expect(hasAfterAllRun).toBe(false);
});

let hasBeforeAllRun2 = false;
let hasAfterAllRun2 = false;

describe('in describe', () => {
beforeAll(() => {
hasBeforeAllRun2 = true;
});

afterAll(() => {
hasAfterAllRun2 = true;
});

test.skip('it should be skipped', () => {
throw new Error('This should never happen');
});
});

test('describe having only skipped test should not run beforeAll', () => {
expect(hasBeforeAllRun2).toBe(false);
});

test('describe having only skipped test should not run afterAll', () => {
expect(hasAfterAllRun2).toBe(false);
});
5 changes: 5 additions & 0 deletions e2e/before-all-skipped/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
10 changes: 5 additions & 5 deletions packages/jest-jasmine2/src/treeProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type TreeNode = {
onException: (error: Error) => void;
sharedUserContext: () => any;
children?: Array<TreeNode>;
} & Pick<Suite, 'getResult' | 'parentSuite' | 'result'>;
} & Pick<Suite, 'getResult' | 'parentSuite' | 'result' | 'markedPending'>;

export default function treeProcessor(options: Options): void {
const {
Expand Down Expand Up @@ -64,11 +64,11 @@ export default function treeProcessor(options: Options): void {
};
}

function hasEnabledTest(node: TreeNode): boolean {
function hasNoEnabledTest(node: TreeNode): boolean {
if (node.children) {
return node.children.some(hasEnabledTest);
return node.children.every(hasNoEnabledTest);
}
return !node.disabled;
return node.disabled || node.markedPending;
}

function wrapChildren(node: TreeNode, enabled: boolean) {
Expand All @@ -78,7 +78,7 @@ export default function treeProcessor(options: Options): void {
const children = node.children.map(child => ({
fn: getNodeHandler(child, enabled),
}));
if (!hasEnabledTest(node)) {
if (hasNoEnabledTest(node)) {
return children;
}
return node.beforeAllFns.concat(children).concat(node.afterAllFns);
Expand Down

0 comments on commit 649796d

Please sign in to comment.