test: wire ViewComponent RSpec helpers via type metadata - #2828
Merged
Conversation
`infer_spec_type_from_file_location!` iterates DIRECTORY_MAPPINGS at call time, but rails_helper registered the `:component` mapping only on the following line, so `spec/components` specs never got `type: :component` metadata from their location. `event_card_component_spec` masked this by declaring the type explicitly; the other two component specs worked around the missing helpers by requiring and including `ViewComponent::TestHelpers` by hand. Register the mapping before inference and include TestHelpers once for `type: :component` specs. ViewComponent 4.x no longer ships its own RSpec auto-wiring (no lib/view_component/rspec.rb), so this belongs in rails_helper. Drop the per-spec boilerplate.
mroderick
marked this pull request as ready for review
August 31, 2026 08:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #2820, addressing Olle's comment: component specs should get
ViewComponent::TestHelpersvia RSpec type metadata instead of requiring and including it by hand.While checking, we found that path-based inference for
type: :componentnever actually worked in this repo.infer_spec_type_from_file_location!iteratesDIRECTORY_MAPPINGSat call time, butspec/rails_helper.rbregistered the:componentmapping only on the following line.event_card_component_specmasked the problem by declaringtype: :componentexplicitly (the view_component generator adds it); the other two component specs patched over the missing helpers with a manualrequire+include.Changes
spec/rails_helper.rb: registerDIRECTORY_MAPPINGS[:component]before callinginfer_spec_type_from_file_location!, and includeViewComponent::TestHelpersonce fortype: :componentspecs. ViewComponent 4.x no longer ships its own RSpec auto-wiring (nolib/view_component/rspec.rb), so this belongs here.spec/components/*: drop the per-specrequire 'view_component/test_helpers'andinclude ViewComponent::TestHelpersboilerplate.Net −4 lines. All 16 component spec examples pass; rubocop clean.