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

beforeAll is being run even when it is inside xdescribe #4487

Closed
Phaze1D opened this issue Sep 15, 2017 · 5 comments
Closed

beforeAll is being run even when it is inside xdescribe #4487

Phaze1D opened this issue Sep 15, 2017 · 5 comments

Comments

@Phaze1D
Copy link

Phaze1D commented Sep 15, 2017

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
beforeAll is being run even when it is inside xdescribe

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

xdescribe('Testing blocking friends', () => {
  beforeAll(done => {
    console.log("This is appearing")
    done()
  })
})

What is the expected behavior?

xdescribe('Testing blocking friends', () => {
  beforeAll(done => {
    console.log("This should not appear")
    done()
  })
})

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

"devDependencies": {
    "jest": "^21.1.0"
}
"jest": {
    "testEnvironment": "node",
    "transform": {
      ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "json"
    ]
  }
@Phaze1D
Copy link
Author

Phaze1D commented Sep 15, 2017

This may be related to #4166

@thymikee
Copy link
Collaborator

Yup, looks like the same thing. I'll just notify @aaronabramov so he's aware of it.

@medikoo
Copy link

medikoo commented Jan 10, 2018

This bug still persist, why this issue was closed?

For those interested, here's workaround patch, that ensures no beforeAll and afterAll are not run in skipped tests or when some others are marked with only.
It decorates global beforeAll and afterAll

Best is to load it in setup test file (one configured through setupTestFrameworkScriptFile setting)

// Decorate treeProcessor to detect whether test is disabled
// (due to other group/test being marked with "only")
const treeProcessor = require.requireActual('jest-jasmine2/build/tree_processor',
  { isInternalModule: true });
const origTreeProcessorFn = treeProcessor.default;
treeProcessor.default = function treeProcessor(options) {
  const runnableIds = options.runnableIds;
  const tree = options.tree;
  const isDirectlyEnabled = (node, parentEnabled) =>
    (node._isEnabled = parentEnabled || runnableIds.indexOf(node.id) !== -1);
  const resolveEnabled = (node, enabled) => {
    if (!node.children || !node.children.length) return;
    let hasChildEnabled = false;
    node.children.forEach(child => {
      resolveEnabled(child, isDirectlyEnabled(child, enabled));
      if (child._isEnabled) hasChildEnabled = true;
    });
    if (!node._isEnabled && hasChildEnabled) {
      node._isEnabled = true;
      node.children.forEach(child => {
        if (child._isBeforeAfterHack) child._isEnabled = true;
      });
    }
  };
  resolveEnabled(tree, isDirectlyEnabled(tree, false));
  return origTreeProcessorFn.call(this, options);
};

// Dummy spec retriever (allows to detect whether parent group is skipped)
const getDummySpec = () => {
  const spec = global.jasmine.getEnv().describe('ignore', Function.prototype);
  spec._isBeforeAfterHack = true;
  spec._parentMarkedPending = spec.markedPending;
  spec.markedPending = true;
  spec.onException = () => {};
  return spec;
};

const decorateBeforeAfter = name => {
  const originalFn = global[name];
  global[name] = function (fn, ...args) {
    const tmpSpec = getDummySpec();
    if (tmpSpec._parentMarkedPending) return null;
    return originalFn.call(this, Object.defineProperty(function (done, ...args) {
      if (tmpSpec._isEnabled === false) {
        if (done) done();
        return null;
      }
      return fn.call(this, done, ...args);
    }, 'length', { configurable: true, value: fn.length }), ...args);
  };
};
decorateBeforeAfter('beforeAll');
decorateBeforeAfter('afterAll');

@tomqwpl
Copy link

tomqwpl commented Sep 9, 2020

This is an issue for me. I naively tried to comment out a block of tests by changing describe to xdescribe, and the beforeAlls still run.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants