cypress-grep icon indicating copy to clipboard operation
cypress-grep copied to clipboard

Code from `before` block is still executed even when there are no tests to be run in the spec.

Open todd-m-kemp opened this issue 3 years ago • 5 comments

Hi Gleb!

I'm loving the grepTags option; this is a really great and something I've been looking for an elegant solution to for quite some time! 🎉

One issue I'm encountering when running filtered tests is what happens with before blocks. Specifically, if there is a spec file that has no tests to be run, the code in the before block is still run, meaning that test setup steps are executed even though no test will actually be run.

I know that using before instead of beforeEach would generally be considered an anti-pattern, but regrettably there are circumstances with testing our application that make using before necessary in some instances.

I'm curious to know your thoughts on whether or not this package an be extended so that it doesn't run code from before blocks when it isn't needed.

todd-m-kemp avatar Jun 16 '21 18:06 todd-m-kemp

Can you try plain Mocha and see what happens? I believe this is built in Mocha behavior that we cannot change from Cypress or at least I don’t see a way to change right now

Sent from my iPhone

On Jun 16, 2021, at 14:20, Todd Kemp @.***> wrote:

 Hi Gleb!

I'm loving the grepTags option; this is a really great and something I've been looking for an elegant solution to for quite some time! 🎉

One issue I'm encountering when running filtered tests is what happens with before blocks. Specifically, if there is a spec file that has no tests to be run, the code in the before block is still run, meaning that test setup steps are executed even though no test will actually be run.

I know that using before instead of beforeEach would generally be considered an anti-pattern, but regrettably there are circumstances with testing our application that make using before necessary in some instances.

I'm curious to know your thoughts on whether or not this package an be extended so that it doesn't run code from before blocks when it isn't needed.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

bahmutov avatar Jun 16 '21 18:06 bahmutov

Looks like you were on the right track. This is current Mocha behaviour, though it is logged as an issue:

https://github.com/mochajs/mocha/issues/2456

There is actually a PR up for it but the review has been quiet for over a year. 😦

https://github.com/mochajs/mocha/pull/4189

I guess there is nothing that can be done here until the Mocha behaviour changes and Cypress updates the bundled version of Mocha accordingly.

Thanks though!

todd-m-kemp avatar Jun 16 '21 18:06 todd-m-kemp

Not proper solution, but my hotfix based on above PR to Mocha

// Taken from https://github.com/mochajs/mocha/pull/4189/files#diff-37b88258822cb207e2caa1a88d4fbc057924da72c5580d1f39b78349214ab622R805
export function calculateNotSkippedTests(suite) {
    const { tests } = suite;
    let total = tests.filter((test) => !test.isPending()).length;
    for (let i = 0; i < suite.suites.length; i += 1) {
        const childSuite = suite.suites[i];
        total += calculateNotSkippedTests(childSuite);
    }
    return total;
}

// ...

before(() => {
    if (!calculateNotSkippedTests(global.mocha.suite)) return; // no test running in this suite, skip before

Strajk avatar Aug 20 '21 20:08 Strajk

according to https://github.com/cypress-io/cypress/issues/24448 it is fixed in latest version

NotSoImportant avatar Nov 15 '21 11:11 NotSoImportant

according to #64 (comment) it is fixed in latest version

Hi there, I have cypress version 9.5.0 but still facing this issue, can you please help ?

HibaBarakat avatar Feb 24 '22 07:02 HibaBarakat

We are in the process of deprecating this repo as this package has been republished under @cypress/grep and has moved to https://github.com/cypress-io/cypress/tree/develop/npm/grep. I'm going to go ahead and close this issue as it appears stale. If this is still an issue, please open a new issue here

jordanpowell88 avatar Oct 28 '22 19:10 jordanpowell88