Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
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 Respostas
Responde à pergunta

0

try using ES6 syntax :

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

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda