Is there a way to distribute my World instance among several step definition files in CucumberJS?

Currently, I am working on implementing a CucumberJS scenario that involves using multiple steps spread out across two separate step definition files. In this setup, the first step establishes certain variables in the World object which need to be accessed by a step defined in another file.

Although the variable is being set correctly, an issue arises when the subsequent step attempts to read it as it returns undefined. Are there any suggestions on how to resolve this dilemma without resorting to merging the step definition files?

For instance:

world.js

var World = function World() {
  this.client = '';
};

module.exports.World = World;

test.feature

Given a variable A
Then some other step

step1.steps.js

module.exports = function () {
    this.World = require(process.cwd() + '/test/features/support/world').World;

    this.Given(/^a Variable A$/, function () {
        this.client = 'abc';
    });
};

step2.steps.js

module.exports = function () {
    this.World = require(process.cwd() + '/test/features/support/world').World;

    this.Then(/^some other step$/, function () {
        console.log(this.client);
    });
};

Answer №1

You need to update this.client to this.World.client. Also, make sure to use an object instead of a constructor in your world.js file:

world.js

module.exports = {
    client: ''
};

step1.steps.js

var world = require('./test/features/support/world.js');

module.exports = function () {
    this.Given(/^a Variable A$/, function () {
        world.client = 'abc';
    });
};

step2.steps.js

var world = require('./test/features/support/world.js');

module.exports = function () {
    this.Then(/^some other step$/, function () {
        console.log(world.client);
    });
};

Answer №2

To enhance your test.feature, consider parameterizing it directly:

Given a variable named "abc"
Then proceed to another step

Next, utilize this variable in your steps:

step1.steps.js

module.exports = function() {
  this.World = require(process.cwd() + '/test/features/support/world').World;

  this.Given(/^a Variable "([^"]*)"$/, function(variable) {
    this.client = variable;
  });
};

step2.steps.js

module.exports = function() {
  this.World = require(process.cwd() + '/test/features/support/world').World;

  this.Then(/^some other step$/, function() {
    console.log(this.client); // output will be abc
  });
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoiding $$hashKey in ngRepeat in AngularJSLearn how to bypass

I'm currently working on creating dynamic tables from an object. Here's the structure of my Object: { categories: ["kids", "home"], home: [{ name: "home 1.1", title: " home 1.2" }, { name: "home 2.1", title: "home 2.2" }, ...

Encountering issues while trying to run npm install for an Angular 7 application, specifically receiving an error stating: "Module not found: @angular-devkit/build-ng-packagr." This error is hindering

I don't have much experience with JavaScript, node, npm, Angular, etc. My expertise lies in TypeScript as I am still a beginner. However, I recently inherited an application that requires maintenance to resolve a cross-site cookie issue. As I attempt ...

The initial execution of the getDocs function may encounter some difficulties

Whenever a user connects from localhost:3000/ (which automatically redirects to /profile), I try to fetch all documents from Firebase. However, it does not work on the first attempt. Strangely, upon refreshing the page, it successfully retrieves the docume ...

Organize the outcomes according to the date provided by the object's keys, and simply tally the number

Hello, I am struggling to retrieve a complex return using MongoDB and Javascript. Can someone please explain how to achieve this? Admin. Group the result by ID. User: Flatten the result. Here is an example of the data: let user = [ { _id: 123, ...

Use the on-screen keyboard to move around by using the arrow keys and choose a value from the list to

I am currently working on developing an on-screen keyboard that allows for navigation using arrow keys and selection with the enter key. My goal is to have each selected key append to an input field. Below is the HTML code I have implemented: <div id ...

MUI Input component does not support the use of the oninput attribute

My MUI Input component is defined like this, but the oninput attribute doesn't seem to work in MUI: <Input variant="standard" size="small" type="number" inputProps={{ min: '0', o ...

Exploring JavaScript through the Lens of Object-Oriented Concepts from Java

Having spent a significant amount of time using Java, I delved into web development with GWT (Google Web Toolkit) where the convenience of having my Java object-oriented constructs translated to GWT seamlessly by Google was a major draw. While my knowledge ...

VueJS can manipulate an inline template by dynamically changing its content and reinitializing

this particular query shares similarities with the discussions on VueJS re-compiling HTML in an inline-template component as well as How to implement Vue js directive in an appended html element Regrettably, the approach suggested in those threads is no l ...

What are some techniques for streamlining and simplifying complex and repetitive JS/jQuery code?

I've come across this code snippet in a JavaScript file that I need to modify, but it's quite confusing to me. I'm not sure why it has been written this way and I would appreciate some help in understanding its purpose. Can someone break it ...

Error in executing a function within an asynchronous function sequence

Whenever a user logs in, I want to update their data in Firestore. However, the code I am using does not seem to work as expected. It fails to create a custom User object from the firebase.User. Can anyone help me understand why this is happening and how ...

In React, when a user clicks the back button on the browser window, ensure you pass props back to the originating component

While moving from component A to component B through routing, I want to send a prop back to A when the user clicks on the browser's back button while on B. Can this be achieved? I am currently using React's history.push for navigating between com ...

The perplexing simplicity of closure

Currently, I am endeavoring to enhance my knowledge of JavaScript closures. Let's consider the following two scenarios in Node.js: function A(){ console.log(data); //this will result in a null pointer } module.exports = function(data){ re ...

Unlocking CORS on DIVSHOT: A Step-by-Step Guide

I've encountered a challenge with the Access-Allow-Control-Origin issue while using Divshot. My mobile app is designed to display posts from a WordPress account, and while testing in a browser I can see the posts without any problem. However, when I t ...

Is there a way to dynamically change an icon based on certain conditions using typescript?

I am struggling with displaying icons in my TypeScript code using Material Icons. I need to dynamically change the icon based on a condition, for example if the power is false I want to display 'power_off', but if it's true then I want to di ...

Is there a way to format wrapped lines with indentation in an unordered list?

Is there a way to indent the wrapped lines of text in an unordered list? The current layout is not quite what I want, as shown in the first image below. Ideally, I would like it to look more like the second image. I attempted to use margin-left: 56px; and ...

Display customized modal content in React based on specific criteria

I have a list of product partners with a "Know More" link that opens a modal. I'm trying to figure out the best way to pass props to the modal in order to display the correct partner's information. Here is the code snippet related to this functi ...

It is imperative that the HTML div element remains within the boundaries of the page

Let me begin by providing some context. I am currently developing a responsive page that uses percentages to position my divs, allowing users to drag and drop items wherever they please. However, a problem arises when the user positions an object too close ...

attempting to fulfil a promise through a resolution

I am currently attempting to use a resolve with a promise in response to an issue with filters that I am currently tackling. However, my resolve function is not yet functioning as expected. I have decided to implement this approach based on advice I recei ...

Is it possible for links to remain clickable even if they pass underneath a div

I have implemented a shadow using a div element to cover some content beneath it. However, I am facing an issue where the div that is under the shadow cannot be interacted with. Is there any solution to this problem? Thank you ...

How to choose the second anchor element in a list with Protractor

I am facing a challenge where I need to click on the 2nd anchor tag in a list of anchor tags. <ul id="shortcuts"> <li><a ui-sref="app.journeyExplorer" href="#/journey-explorer/"><span class="ng-binding">1</span></a> ...