question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Execution order of `after` hooks when globally defined is not direct opposite of `before` hooks execution order

See original GitHub issue

Current behavior:

In before hooks, the hooks defined in support/index.js execute first, before any hooks defined in the spec file, but in the after hooks, the hooks defined in support/index.js do not execute last - the after hooks defined in the spec file run last.

I verified this is not a UI only issue - this is how they execute.

Desired behavior:

This behavior is not what I was expecting. I would expect the after hooks to execute in the exact opposite as before hooks had - in the same manner. Maybe I’m missing something.

This seems to be how they’ve always run, tested back to 3.4.1.

Test code to reproduce

support/index.js

before(() => {
  cy.log('support/index.js before')
  console.log('support/index.js before')
})

beforeEach(() => {
  cy.log('support/index.js beforeEach')
  console.log('support/index.js beforeEach')
})

afterEach(() => {
  cy.log('support/index.js afterEach')
  console.log('support/index.js afterEach')
})

after(() => {
  cy.log('support/index.js after')
  console.log('support/index.js after')
})

spec.js

before(() => {
  cy.log('main before')
  console.log('main before')
})

beforeEach(() => {
  cy.log('main beforeEach')
  console.log('main beforeEach')
})

describe('my tests', () => {
  before(() => {
    cy.log('suite before')
    console.log('suite before')
  })
  
  beforeEach(() => {
    cy.log('suite beforeEach')
    console.log('suite beforeEach')
  })
  
  it('visit', () => {
    cy.log('test body')
    console.log('test body')
  })

  afterEach(() => {
    cy.log('suite afterEach')
    console.log('suite afterEach')
  })

  after(() => {
    cy.log('suite after')
    console.log('suite after')
  })
})

afterEach(() => {
  cy.log('main afterEach')
  console.log('main afterEach')
})

after(() => {
  cy.log('main after')
  console.log('main after')
})
Screen Shot 2021-02-05 at 3 19 17 PM

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rachelrudermancommented, Jun 13, 2022

I’ve added a reproducible example in Cypress 10 here

1reaction
cameroncooks-branchcommented, Jun 10, 2022

Still seeing this in cypress 9.6.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to define Execution Order of Hooks in Cucumber - Tools QA
Order hooks to run in a particular sequence is easy to do. As we already know the way to specify hooks in cucumber-like...
Read more >
Hooks - SpecFlow's documentation
Hooks are global, but can be restricted to run only for features or scenarios by defining a scoped binding, which can be filtered...
Read more >
Hooks - Sequelize
Hooks (also known as lifecycle events), are functions which are called before and after calls in sequelize are executed. For example, if you...
Read more >
Order of execution for event functions - Unity - Manual
Script lifecycle overview · First Scene Load · Editor · Before the first frame update · In between frames · Update order ·...
Read more >
Ruby cucumber scenario hook execution order - Stack Overflow
I have an After hook that gets executed before a hook on the scenario itself. I want to reverse the order of those...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found