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

Mocha's afterEach is missing "this" #3895

Closed
cbromberg opened this issue Apr 5, 2019 · 5 comments
Closed

Mocha's afterEach is missing "this" #3895

cbromberg opened this issue Apr 5, 2019 · 5 comments
Labels
stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period

Comments

@cbromberg
Copy link

cbromberg commented Apr 5, 2019

Current behavior:

In mocha it's documented behaviour that you can access this, which in turn gives access to currentTest. See Mocha.IBeforeAndAfterContext; just like in beforeEach.

With Cypress intercepting this and inserting the screenshooter there, it misses to pass on the this project, here's the code I found in the debugger:

_afterEach(name, function () {
    onFailed.call(this)
    fn()
})

where fn is the user-defined afterEach function

Desired behavior:

this should be passed on. fn.call(this) would achieve that (in the context of the code above).

Steps to reproduce: (app code and test code)

context('Mocha afterEach', () => {
    beforeEach(function() {
        expect(this).to.be.not.undefined
        expect(this).to.have.property('currentTest')
    })

    afterEach(function() {
        expect(this).to.be.not.undefined
        expect(this).to.have.property('currentTest')
    })

    it('This', () => {
        expect(this).to.be.not.undefined
        expect(this).to.have.property('test')
    })
})

Versions

Cypress 3.2.0

@Lakitna
Copy link
Contributor

Lakitna commented Apr 8, 2019

This might be because Cypress 3.x is using an old version of Mocha (3.5.3 where latest = 6.1.1). This has been upgraded in the 4.0 branch.

Edit: It has been upgraded to Mocha 5.x

@jennifer-shehane
Copy link
Member

Closing this as duplicate of #2528 @chrisbreiding Can you verify this will be fixed with Mocha upgrade?

@jennifer-shehane jennifer-shehane added the type: duplicate This issue or pull request already exists label Apr 24, 2019
@jennifer-shehane
Copy link
Member

This is no longer undefined after the Mocha upgrade, but there is a circular reference error thrown when used in this way.

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Test'
    |     property 'commands' -> object with constructor 'Array'
    |     index 0 -> object with constructor 'Object'
    |     property 'actual' -> object with constructor 'Context'
    --- property 'currentTest' closes the circle

Because this error occurred during a 'after each' hook we are skipping the remaining tests in the current suite: 'Mocha afterEach'

spec.js

context('Mocha afterEach', () => {
  beforeEach(function () {
    expect(this).to.be.not.undefined
    expect(this).to.have.property('currentTest')
  })

  afterEach(function () {
    expect(this).to.be.not.undefined
    expect(this).to.have.property('currentTest')
  })

  it('This', () => {
    expect(this).to.be.not.undefined
    expect(this).to.have.property('test')
  })
})

3.8.3

Screen Shot 2020-01-29 at 3 41 01 PM

4.0.0

Screen Shot 2020-01-29 at 3 39 51 PM

@jennifer-shehane jennifer-shehane removed the type: duplicate This issue or pull request already exists label Jan 29, 2020
@cypress-bot cypress-bot bot added the stage: needs investigating Someone from Cypress needs to look at this label Jan 29, 2020
@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label May 18, 2023
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period
Projects
None yet
Development

No branches or pull requests

5 participants
@jennifer-shehane @cbromberg @Lakitna @cypress-app-bot and others