timjroberts / cucumber-js-tsflow

Provides 'specflow' like bindings for Cucumber.js in TypeScript 1.7+.
MIT License
134 stars 35 forks source link

Tags in beforeAll decorator not respected #134

Closed bryant-svedin-gehc closed 1 year ago

bryant-svedin-gehc commented 1 year ago

I have several beforeAll decorators with different tags. When running npx cucumber-js -p default --tags=@foo all given/when/then/before/after work as expected, EXCEPT the beforeALL steps. It will run all beforeAll regardless of tags in decorator input. For example, the below steps would run both beforeAll but only the foo before. I have not checked if this problem also exists in afterAll because I don't currently use it but after decorator does respect tag.

public async beforeAllFeatureFoo(): Promise<void> {
  // Do a few foo checks
}

@beforeAll({ tag: '@bar' })
public async beforeAllFeatureBar(): Promise<void> {
  // Do a few bar checks
}

@before({ tag: '@foo' })
public async beforeAllScenarioFoo(): Promise<void> {
  // Do a few foo setups
}

@before({ tag: '@bar' })
public async beforeAllScenarioBar(): Promise<void> {
  // Do a few bar setups
}
Fryuni commented 1 year ago

Cucumber's beforeAll and afterAll hooks do not support tags at all. They are executed before/after the entire test suite while tags in Cucumber are processed per scenario, so they are not available for those hooks.

We reused the types when defining our decorators incorrectly, I'll get this fixed right away, thanks for the report.


If you want to conditionally run global hooks you'd need to use some other mechanism, like checking an environment variable yourself:

@beforeAll()
public async beforeAllScenarioFoo(): Promise<void> {
  if (!process.env.FOO) return;
  // ...
}

Then run with FOO=1 npx cucumber-js -p default.

This looks like an interesting use case for tags, but it would be a feature request on CucumberJS.

bryant-svedin-gehc commented 1 year ago

Thanks for your quick response and suggested workaround