cucumber / cucumber-js

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

Step undefined when we using api function #2431

Closed obouhlel closed 1 week ago

obouhlel commented 2 months ago

πŸ‘“ What did you see?

With this code:

import { runCucumber, loadConfiguration, loadSupport } from '@cucumber/cucumber/api'

async function run(name) {
    const { runConfiguration } = await loadConfiguration({ file: `./config/${name}.json` })

    const env = {
        cwd: process.cwd(),
        stdout: process.stdout,
        stderr: process.stderr,
        debug: false
    }

    const support = await loadSupport(runConfiguration, env)
    console.log(support)

    await runCucumber({ ...runConfiguration, support }, env, (message) => {
        (message)
    })
}

run("add").then(() => run("sub"))

The second time I run the scenario (or any subsequent runs), the steps are undefined. When I log the support variable using console.log, the stepDefinitions attribute is empty.

βœ… What did you expect to see?

I expect the support variable to retain the stepDefinitions attribute.

πŸ“¦ Which tool/library version are you using?

node: 18.20.4 cucumber: 11.0.1

πŸ”¬ How could we reproduce it?

In this repository, I have created a simple test to reproduce this issue.

  1. npm install
  2. npm run test

πŸ“š Any additional context?

No response

davidjgoss commented 2 months ago

I expect the support variable to retain the stepDefinitions attribute.

Your support variable only exists in the scope of your run function though, so each invocation tries to create it afresh, but you're in the same overall Node.js process so it only works the first time.

Assuming your support code is consistent across your runs, then you should be able to call loadSupport just once upfront, before any of your calls to run, and pass in your support object each time.