Cypress: 'this' is undefined

Created on 30 Jul 2019  路  2Comments  路  Source: cypress-io/cypress

Steps to reproduce: (app code and test code) and Current Code

describe('Check Broken links', ()=>{
    beforeEach(function() {
        cy.visit('https://www.cypress.io/').get('a').as('links')
    })
    it('prints handler', ()=> {
       console.log(this.links);
    });
});

package.json

{
  "name": "cypress_test",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "cypress:open": "cypress open"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "cypress": "^3.4.1"
  }
}

Error received

image

Desired behavior:

it should print the "links" in the console

Most helpful comment

https://on.cypress.io/variables-and-aliases#Sharing-Context
You can not access mocha context from arrow function, please use es5 function syntax:

it('has access to text', function () {
  this.text // is now available
})

All 2 comments

https://on.cypress.io/variables-and-aliases#Sharing-Context
You can not access mocha context from arrow function, please use es5 function syntax:

it('has access to text', function () {
  this.text // is now available
})

@Shelex Thanks for the response. I replaced arrow function with the ES5 syntax and it worked

Was this page helpful?
0 / 5 - 0 ratings