Skip to content
Advertisement

Context eval expression when I write function in Mocha Test as lambda

When I use the below code, I do not get the context eval error for the variable data

 describe('Hooks',()=>{
    before(()=>{
        cy.fixture("example.json").then(function(data){
            this.data=data
        })
    })

    it("my First Test case", function(){
        cy.visit("https://rahulshettyacademy.com/angularpractice/")
        
        cy.get(":nth-child(1) > .form-control").type(this.data.name)
       
        cy.get("#exampleFormControlSelect1").select(this.data.gender)
    })
})

When write my mocha test function as lambda (Refer code below), I receive context eval error for data variable Code

describe('Hooks',()=>{
    before(()=>{
        cy.fixture("example.json").then((data)=>{
            this.data=data
        })
    })

    it("my First Test case", () =>{
        cy.visit("https://rahulshettyacademy.com/angularpractice/")
        
        cy.get(":nth-child(1) > .form-control").type(this.data.name)
       
        cy.get("#exampleFormControlSelect1").select(this.data.gender)
    })
})

Error

at Context.eval (webpack:///cypress/integration/examples/Test8Framework.js:4:13) From previous event: at Context.thenFn (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:155190:24) at Context.then (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:155629:22) at Context. (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:170229:22) at (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169653:16) From previous event: at runCommand (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169632:9) at next (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169778:15) at (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169806:17) From previous event: at next (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169778:35) From previous event: at Promise.catch.err.name (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169819:38) From previous event: at run (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169812:22) at $Cy.cy. [as fixture] (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:170269:12) at Context.runnable.fn (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:170496:22) at callFn (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:104252:22) at Hook…/driver/node_modules/mocha/lib/runnable.js.Runnable.run (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:104239:8) at (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:176219:29) From previous event: at Object.onRunnableRun (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:176207:18) at $Cypress.action (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:166637:29) at Hook.Runnable.run (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:174384:14) at next (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:104754:11) at (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:104798:6) at timeslice (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:98724:28)

Advertisement

Answer

If you store and access the fixture data using this test context object, make sure to use function () { … } callbacks. Otherwise the test engine will NOT have this pointing at the test context.

This is mentioned in the cypress docs. Hence in your test where you’re using it("my First Test case", () =>{ it is failing and when you’re using it("my First Test case", function(){ it is getting passed.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement