cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Before() is executed even if it's parent is filtered out

Open ghost opened this issue 4 years ago • 24 comments

With this spec file:

describe('Cypress grep', {
  tags: '@smoke'
}, () => {
  before(() => {
      expect(false).to.equal(true)
  })
  it('succeeds', () => {
    expect(true).to.equal(true)
  })
})

When executing cypress with --env grepTags=-@smoke, the test fails because the before() function is executed even though the whole describe is not supposed to be executed.

ghost avatar Aug 19 '21 09:08 ghost

Not proper solution, but my hotfix

// 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

This issue is also reproductible for after() hook

wmedali avatar Sep 20 '21 22:09 wmedali

In my case I have a describe block with an before() hook, followed by beforeEach(), then some it() blocks, and finally an after() hook. Strangely, grep runs before() and after() hook in some specs, while it does not in other specs

nitishSr avatar Sep 21 '21 09:09 nitishSr

We also have this problem. I think the best solution for the moment is to just lock the version to cypress-grep 2.5.1, which does not have this issue.

nicolajv avatar Nov 02 '21 14:11 nicolajv

In my case I have a describe block with an before() hook, followed by beforeEach(), then some it() blocks, and finally an after() hook. Strangely, grep runs before() and after() hook in some specs, while it does not in other specs

I got a workaround for this, by applying a check for the "grepTags" env variable has a value set in it at the start of before() and after() hook

nitishSr avatar Nov 02 '21 14:11 nitishSr

We upgraded to the new version since grepOmitFiltered actually works to bypass this issue: https://github.com/cypress-io/cypress-grep/pull/84

nicolajv avatar Nov 04 '21 09:11 nicolajv

Facing the same issue, both before and after block get executed even if the describe block is meant to filter out

describe("describe block 1", {tags:"@sanity"} ,function(){
    it("it block 1 ", function(){
        cy.log("dec block 1 it block")
    })
})
describe("describe block 2 ",{tags:"@regression"},function(){
    before("before block", function(){
        cy.log("before")
        cy.wait(10000)
    })
    it("it block 2",function(){
        cy.log("it block 2")
    })
    it("it block 3",function(){
        cy.log("it block 3")
    })
    after("After block", function(){
        cy.log("after")
        cy.wait(10000)
    })
})

With --env grepTags=@sanity , expected only "it block 1" to be executed rest all it, before and after block to be filter out/skipped

royarunendu avatar Nov 17 '21 12:11 royarunendu

My issue is when I'm using .skip in a test not tagged, cause it still runs the beforeAll and afterAll. I'm on version 2.10.2.

If I run with a tag --env grepTags=@grep to not include the specs below, spec 1 is not executed while the second one is and fails on the before(). Using grepFilterSpecs or grepOmitFiltered doesn't seem to make any difference.

Spec 1

describe('1-Test Untagged', function (){
    before(() => {
        expect(false).to.equal(true)
    })
    it('1-Test log', function () {
        cy.log("block 1")
    })
})

Spec 2

describe('2-Test Untagged With Skipped Test', function(){
    before(() => {
        expect(false).to.equal(true)
    })
    it.skip('1-Test log', function () {
        cy.log("block 1")
    })
})

rennergimenes avatar Nov 18 '21 04:11 rennergimenes

Same issue, the latest version The grep filter is not working when there's a before() or beforeall() or after() block

gvillafuertegrid avatar Feb 03 '22 22:02 gvillafuertegrid

We upgraded to the new version since grepOmitFiltered actually works to bypass this issue: cypress-io/cypress-grep#84

@nicolajv I'm assuming that doesn't work for negative filters... since grepOmitFiltered doesn't support it?

Hypercubed avatar Feb 04 '22 20:02 Hypercubed

When I run below spec with this command: npx cypress run --env=grepTags=@smoke, 2nd test is getting passed as expected but the 1st test is FAILING instead of going into pending/skipped state. Need your help on this

describe('grand',{tags: '@ha-smoke'}, () => { before(() => { expect(true).to.be.false }) it('runs', () => {}) })

describe('top',{tags: '@smoke'}, () => { it('runs too', () => {}); })

harsha333 avatar Oct 21 '22 06:10 harsha333

With this spec file:

describe('Cypress grep', {
  tags: '@smoke'
}, () => {
  before(() => {
      expect(false).to.equal(true)
  })
  it('succeeds', () => {
    expect(true).to.equal(true)
  })
})

When executing cypress with --env grepTags=-@smoke, the test fails because the before() function is executed even though the whole describe is not supposed to be executed.

Even I'm facing the same, Have you fixed this? Any suggestions?

harsha333 avatar Oct 21 '22 06:10 harsha333

I have the same issue I believe, I pass something like --env grepTags=@login and it will run those tagged tests but it runs the before hook from a couple unrelated test suites no matter which tag I use.

GeorgeXCV avatar Feb 28 '23 10:02 GeorgeXCV

Any updates on this? It is a very problematic issue

Phonesis avatar Jun 22 '23 11:06 Phonesis

any updates on this ???

halilibrahimayhan avatar Jul 11 '23 14:07 halilibrahimayhan

any updates on this ??? @nagash77 @jordanpowell88

QuintilianoNery avatar Jul 11 '23 21:07 QuintilianoNery

any updates on this ?

@jordanpowell88 @nagash77

ceciliamariar avatar Jul 21 '23 14:07 ceciliamariar

any update here? facing same issue

begona-paella avatar Sep 20 '23 09:09 begona-paella

any update here? We can't use grep till this is resolved

rudreshtrivedi avatar Dec 04 '23 09:12 rudreshtrivedi

Do we have any update here?

sosafe-deepika-arneja avatar Apr 03 '24 15:04 sosafe-deepika-arneja

Any updates on this?

dannylistSR avatar May 01 '24 14:05 dannylistSR

@cypress-github-team-services-sa I also have a problem with this in the latest Cypress v13.9.0 Instead of skipping tests that do not have a specific tag, the test runs afterAll for a given describe block.

For me, this greatly limits the possibility of using tags, Cypress recommends before/after blocks. Please make this a higher priority.

krzysieknd avatar May 08 '24 14:05 krzysieknd

Any updates here? I can still repro this issue. Thanks!

ashleympascoe avatar Jun 11 '24 16:06 ashleympascoe

This would be a great one for us to be fixed

AshMcConnell avatar Jun 27 '24 17:06 AshMcConnell

Any updates here? facing same issue

YanBai01 avatar Aug 01 '24 06:08 YanBai01