Contents

World in Hooks

Background:

  • Given a file named "features/a.feature" with:
    gherkin
    Feature: some feature
      Scenario: some scenario
        Given a step
  • And a file named "features/step_definitions/cucumber_steps.js" with:
    ruby
    const {Given} = require('@cucumber/cucumber')
    Given(/^a step$/, function() {})
  • And a file named "features/support/world.js" with:
    ruby
    const {setWorldConstructor} = require('@cucumber/cucumber')
    function WorldConstructor() {
      return {
        isWorld: function() { return true }
      }
    }
    setWorldConstructor(WorldConstructor)

Scenario: World is this in hooks

  • Given a file named "features/support/hooks.js" with:
    ruby
    const {After, Before } = require('@cucumber/cucumber')
    Before(function() {
      if (!this.isWorld()) {
        throw Error("Expected this to be world")
      }
    })
    After(function() {
      if (!this.isWorld()) {
        throw Error("Expected this to be world")
      }
    })
  • When I run cucumber-js
  • Then it passes

Scenario: World is this in BeforeStep hooks

  • Given a file named "features/support/hooks.js" with:
    ruby
    const {BeforeStep } = require('@cucumber/cucumber')
    BeforeStep(function() {
      if (!this.isWorld()) {
        throw Error("Expected this to be world")
      }
    })
  • When I run cucumber-js
  • Then it passes

Scenario: World is this in AfterStep hooks

  • Given a file named "features/support/hooks.js" with:
    ruby
    const {AfterStep } = require('@cucumber/cucumber')
    AfterStep(function() {
      if (!this.isWorld()) {
        throw Error("Expected this to be world")
      }
    })
  • When I run cucumber-js
  • Then it passes

Source: features/world_in_hooks.feature at commit 3ac6ecda, Cucumber Js (MIT license).