cypress-io / cypress-grep

Filter tests using substring
137 stars 19 forks source link

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

Closed todd-m-kemp closed 1 year ago

todd-m-kemp commented 3 years ago

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.

bahmutov commented 3 years ago

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.

todd-m-kemp commented 3 years ago

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!

Strajk commented 3 years ago

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
NotSoImportant commented 2 years ago

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

HibaBarakat commented 2 years ago

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 ?

jordanpowell88 commented 1 year ago

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