cucumber/cucumber-jvm-scala

Hooks can silently be ignored if body returns an Int

gaeljw opened this issue · 3 comments

Describe the bug
The following hook definition:

Before {
  someMethodThatReturnInt()
}

will NOT define a hook because it will be interpreted as Before(someMethodThatReturnInt()) and a missing body.

This it not exactly a bug but this can be quite confusing for users.

To Reproduce

object MySteps extends ScalaDsl with EN {
  Before {
    println("Before hook")
    1 // Silly but for the reproduction purpose
  }
}

Create a feature file with 2 scenarios and a dumb step definition. Check that the before hook is printed only once.

Expected behavior
I would expect that a warning/error is raised at compile time, or that the body is actually interpreted as the hook body rather than something providing the order of the hook.

Depending on how we can fix that, we will consider other options.

Versions:

  • Cucumber Scala: 5.7.0
  • Scala: 2.12

Additional context

  • To be more confusing, I think it will work as a side effect when hooks are defined in class but not in objects because glue classes are reinstantiated at each scenario while glue objects are not.

  • Other definitions (like ParameterType) should be checked as well..

For anyone coming here, a workaround is to explicitly type your body as returning Unit like:

Before {
  someMethodThatReturnInt()
  ()
}

This happens since we enabled hooks to be defined with by name parameter (#26)

I believe there is no other way than raising an exception at runtime in such cases without introducing a breaking change on the syntax.

Considering this will happen with hooks defined like this that returns Int or String (others would be caught at compile time), I think this is acceptable even if not ideal.