World object in cucumber.js or where to put state in cucumber tests
<p>I'm trying to save the current navigation state in one step (the page on a platform with multiple websites) in cucumber.js so the following steps of a scenario can deal with it. I thought using the World object for it, but mysterious things are happening.</p>

<p>I have a navigation state object like this:</p>

<pre><code>module.exports = {
    pageName:null,
    siteName: null,
    isLoggedIn: false
}
</code></pre>

<p>Then I have a NavigationStateManager like this</p>

<pre><code>function NavigationStateManager() {
    var state
    this.setState = function(stateP) {
       state = stateP
    }
    this.setPage = function(pageNameP, siteNameP, isLoggedInP) {
        // among other things do something link this:
        state.pageName = pageNameP
        state.siteName = siteNameP
        state.isLoggedIn = isLoggedInP
    }
}
</code></pre>

<p>And I have a World object</p>

<pre><code>var navState = require('./navigation-state')
var NavigationStateManager = require('./navigation-state-manager')

var navigationStateManager = new NavigationStateManager()

function World() {
    this.navState = simpleCopy(navState)
    navigationStateManager.setState(this.navState)
}

function simpleCopy(objectToCopy) {
    var copy = {}
    for(var key in objectToCopy) {
       copy[key] = objectToCopy[key]
    }
    return copy
}
</code></pre>

<p>In my steps file I do this</p>

<pre><code>var World = require('../support/world')

module.exports = function() {
   this.World = World

   this.Given(...)
   this.Then(...)
}
</code></pre>

<p>For some reason the state becomes undefined in the NavigationStateManager when the Given steps have been executed and the Then steps are being executed. When I log I can't see setState being called with an 'undefined' argument. I've had a different setup, putting the NavigationStateManager on the World object, but it gave me similar issues. Apparently the World object doesn't remain the same through all steps of a scenario, but how does it behave. The error seems to go against all JavaScript knowledge I have. Where do I put state in my tests?</p>