Skip to content

Fix fixture declarations not propagating to auto-included shared example groups#33

Merged
ngan merged 1 commit intomainfrom
np-fix-shared-example-fixture-propagation
Mar 1, 2026
Merged

Fix fixture declarations not propagating to auto-included shared example groups#33
ngan merged 1 commit intomainfrom
np-fix-shared-example-fixture-propagation

Conversation

@ngan
Copy link
Collaborator

@ngan ngan commented Mar 1, 2026

Summary

Fixes a bug where fixture declarations are invisible to examples inside shared example groups that are auto-included via config.include_context with metadata filters.

Root cause: When config.include_context "shared examples", :some_metadata is configured, RSpec creates those child groups during configuration — before the spec file's fixture call sets the declaration on the parent group's metadata. Since RSpec snapshots parent metadata into child groups at creation time (metadata.update(parent_metadata)), the child's metadata never includes the :fixture_kit_declaration key. The prepend_before(:example, DECLARATION_METADATA_KEY) hook never fires for examples in those groups, leaving @_fixture_kit_repository nil.

Fix: Replace the direct metadata[DECLARATION_METADATA_KEY] = declaration with update_inherited_metadata(DECLARATION_METADATA_KEY => declaration). This is RSpec's own mechanism for pushing metadata updates — it sets the metadata on the current group AND recursively propagates to all existing children and their examples.

Real-world impact: This bug affects any RSpec spec where shared examples are globally auto-included via metadata filters. For example, at Gusto, all GraphQL mutation specs auto-include authorization shared examples via config.include_context 'applies mutation authorization correctly', :subgraph. Any mutation spec that uses fixture hits this bug.

Changes

  • lib/fixture_kit/rspec.rb: Replace direct metadata assignment with update_inherited_metadata to propagate fixture declarations to already-created child groups
  • spec/dummy/spec/integration/fixture_kit_integration.rb: Add integration test reproducing the exact pattern — shared examples auto-included via config.include_context with metadata, host group declares fixture
  • spec/integration/dummy_app_spec.rb: Add expected assertion markers for the new test (RSpec-only, since shared examples are an RSpec concept)
  • spec/unit/rspec_entrypoint_spec.rb: Add update_inherited_metadata to the test stub

Test plan

  • New integration test reproduces the bug (fails without fix, passes with fix)
  • All 157 existing tests pass
  • Host group examples still work alongside shared example group examples
  • Verified fix against real-world Gusto zenpayroll spec with auto-included mutation authorization shared examples (26 examples, 0 failures)

🤖 Generated with Claude Code

@ngan ngan force-pushed the np-fix-shared-example-fixture-propagation branch 4 times, most recently from 940eb7f to 3f8fb92 Compare March 1, 2026 06:18
…ple groups

When shared example groups are auto-included via `config.include_context`
with metadata filters, RSpec creates those child groups during
configuration — before the spec file's `fixture` call sets the
declaration on the parent group's metadata. Since RSpec snapshots parent
metadata into child groups at creation time, the child's metadata never
includes the fixture declaration, and the `prepend_before` hook never
fires for those examples.

The fix uses RSpec's own `update_inherited_metadata` to propagate the
fixture declaration to any child groups that already exist when `fixture`
is called. This is the same mechanism RSpec uses internally to push
metadata updates to descendants.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ngan ngan force-pushed the np-fix-shared-example-fixture-propagation branch from 3f8fb92 to f2b8d3d Compare March 1, 2026 06:20
@ngan ngan merged commit ad81500 into main Mar 1, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant