Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
CucumberJS - how to use world object "this" outside of the given, when and then architecture

I was working on cucumber testing and met the issue that I don't know how to get the world object "this" outside of cucumber architecture

For example,

function verifyFunction(){
  expect(this.a).toEqual(1);
}


Given(
  /^bla bla bla$/, function () {
    this.a = 1
  }
);

Then(
  /^bla bla bla$/, function () {
    verifyFunction()
  }
);

When running the example test like above, it will show the error Cannot read property 'a' of undefined.

Any idea about how to solve this issue?

Thanks a lot!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

try using ES6 syntax :

const verifyFunction = () =>{
 expect(this.a).toEqual(1);
} 
about 4 years ago · Juan Pablo Isaza Report

0

Caveat: I only use Cucumber with ruby.

Generally when using functions from step refs you should pass things into the function and retrieve things from the function. In ruby you would share between steps using a global. So

module StepHelpers
  def get_foo
    return 'foo'
  end

  def bar(x)
    puts x
  end
end
World StepHelpers

Given 'foo' do
  @foo = get_foo
end

When 'bar with foo' do
  # We can access @foo because its defined as a global in a step
  bar(@foo)
end

Try translating the above into your js cukes

n.b. the module stuff is to prevent the methods getting into your applications global namespace.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!