cucumber / cucumber/godog

Scope of Before/After Hooks in GoDog ScenarioContext Affecting All Scenarios

Open
#683 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
2.7k
Forks
280
Avg merge
2d 20h
Merged PRs (30d)
2

Description

### 👓 What did you see?

In my test setup, I have a main FeatureContext that registers scenarios and a nested AbcFeatureContext that also registers its own scenarios and hooks.

```
func FeatureContext(ctx context.Context, s *godog.ScenarioContext, log logger.Logger) {
// Main steps and hooks
AbcFeatureContext(ctx, s)
}

func AbcFeatureContext(ctx context.Context, s *godog.ScenarioContext) {
s.Before(func(ctx context.Context, _ *godog.Scenario) (context.Context, error) {
// Pre-checks specific to AbcFeatureContext
return ctx, nil
})

// Steps specific to AbcFeatureContext

s.After(func(ctx context.Context, _ *godog.Scenario, _ error) (context.Context, error) {
// Cleanup specific to AbcFeatureContext
return ctx, nil
})
}
```
I have noticed that the Before and After hooks defined in AbcFeatureContext are being executed for all scenarios registered in FeatureContext, not just for those specific to AbcFeatureContext.

### ✅ What did you expect to see?

I expected the hooks in AbcFeatureContext to apply only to scenarios that are meant to be part of AbcFeatureContext and not to every scenario registered under FeatureContext.

### 📦 Which tool/library version are you using?

go 1.23.0
godog v0.14.1

### 🔬 How could we reproduce it?

_No response_

### 📚 Any additional context?

Also let me know if this is expected behaviour and you have any workaround for it?

Contributor guide

Open the contributing guide

Research direction

The payload names no repository files or tests. Start by tracing how ScenarioContext.Before and After registrations from FeatureContext and AbcFeatureContext are executed, then inspect existing hook-related tests. Confirm the intended hook scope with maintainers; done should be a documented behavior or an agreed scoped-hook change with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.