cucumber / cucumber-js

Cucumber for JavaScript
https://cucumber.io
MIT License
5.07k stars 1.09k forks source link

world parameters are mutable from scenarios, and changes leak to other scenarios #2356

Closed mrsenzy closed 11 months ago

mrsenzy commented 11 months ago

👓 What did you see?

For below scenarios, when i try to assign color to world parameter from first scenario as blue, it get retained until the red color from second scenario get assigned.

Scenario: Will pass Given my color is "blue" Then my color should not be red

Scenario: Will fail Given my color is "red" Then my color should not be red

✅ What did you expect to see?

When world is isolated for each scenario, shouldn't the parameters.color get undefined for second scenario before getting assigned with color red. Currently it is retaining the color blue from previous scenario

📦 Which tool/library version are you using?

@cucumber/cucumber: 10.0.1

davidjgoss commented 11 months ago

Could you elaborate by providing your step definition code and configuration?

mrsenzy commented 11 months ago

Step definition:

Given("my color is {string}", async function(this: CustomWorld, color: string) {
    console.log("Given Color: ", this.parameters.color)
    this.parameters.color = color;
    console.log("Given Color after assigning: ", this.parameters.color)
  })

  Then("my color should not be red", async function(this: CustomWorld) {
    console.log("Then Color: ", this.parameters.color)
    let favColor = this.parameters.color

    if(favColor!="blue"){
        logger.error("Assertion failed: ",favColor=="blue")
        console.trace('trace')
    }
    expect(favColor).toStrictEqual("blue")
  })
let common = [
    './e2e/features/**/*.feature',
    '--require-module ts-node/register',
    '--require ./e2e/stepDefinitions/**/*.ts',
    '--require ./cucumber.config.ts',
    '--format json:./report/report.json',
    '--format html:./report/cucumberjs-report.html',
    '--format progress-bar',
    '--format @cucumber/pretty-formatter',
    '--format rerun:@failedScenarios.txt',
].join(' ');
davidjgoss commented 11 months ago

Thanks @mrsenzy. So, yeah, currently the world parameters are mutable from user code.

The normal pattern is to use world parameters as a source of arbitrary configuration, and set state on the world itself e.g. this.color = 'red' - however it's important to have guard rails. I think we should address this by:

davidjgoss commented 11 months ago

Somewhat related to https://github.com/cucumber/cucumber-js/issues/2308, in that probably most things we pass to user code should be immutable (practically if not physically).

davidjgoss commented 11 months ago

Released in https://github.com/cucumber/cucumber-js/releases/tag/v10.1.0