From 312100fda996ef0c9c788adf36dacb9679c530b4 Mon Sep 17 00:00:00 2001 From: Morgan Roderick Date: Mon, 31 Aug 2026 10:46:58 +0200 Subject: [PATCH] test: wire ViewComponent RSpec helpers via type metadata `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. --- spec/components/chapter_picker_component_spec.rb | 3 --- spec/components/chapters_sidebar_component_spec.rb | 2 -- spec/components/event_card_component_spec.rb | 2 -- spec/rails_helper.rb | 13 ++++++++----- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/spec/components/chapter_picker_component_spec.rb b/spec/components/chapter_picker_component_spec.rb index b6efaf2a1..81ebf2f07 100644 --- a/spec/components/chapter_picker_component_spec.rb +++ b/spec/components/chapter_picker_component_spec.rb @@ -1,9 +1,6 @@ require 'rails_helper' -require 'view_component/test_helpers' RSpec.describe ChapterPickerComponent do - include ViewComponent::TestHelpers - let(:chapters) { Fabricate.times(3, :chapter) } it 'renders a text input with datalist attributes' do diff --git a/spec/components/chapters_sidebar_component_spec.rb b/spec/components/chapters_sidebar_component_spec.rb index 73bb65e5a..9a05797be 100644 --- a/spec/components/chapters_sidebar_component_spec.rb +++ b/spec/components/chapters_sidebar_component_spec.rb @@ -1,8 +1,6 @@ require 'rails_helper' -require 'view_component/test_helpers' RSpec.describe ChaptersSidebarComponent do - include ViewComponent::TestHelpers include Rails.application.routes.url_helpers let(:chapters) { Fabricate.times(3, :chapter) } diff --git a/spec/components/event_card_component_spec.rb b/spec/components/event_card_component_spec.rb index 9eae9efc2..e5a5a0bd6 100644 --- a/spec/components/event_card_component_spec.rb +++ b/spec/components/event_card_component_spec.rb @@ -1,8 +1,6 @@ require 'rails_helper' -require 'view_component/test_helpers' RSpec.describe EventCardComponent, type: :component do - include ViewComponent::TestHelpers let(:chapter) { Fabricate(:chapter, active: true) } context 'with a workshop' do diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 069b8d2d4..f362e8cdb 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -29,13 +29,16 @@ config.use_transactional_fixtures = true # RSpec Rails can automatically mix in different behaviours based on the - # file location of the spec. This line needs to be present for ViewComponent - # to work with controller specs + # file location of the spec. + # Register the spec/components directory mapping BEFORE inference runs — + # `infer_spec_type_from_file_location!` snapshots DIRECTORY_MAPPINGS at call time. + RSpec::Rails::DIRECTORY_MAPPINGS[:component] = %w[spec components] config.infer_spec_type_from_file_location! - # Connect ViewComponent to RSpec, adding RSpec metadata type: :component. - # This extends Rails' own built-in (e.g. "type: :controller") metadata system. - RSpec::Rails::DIRECTORY_MAPPINGS[:component] = %w[spec/components] + # ViewComponent does not wire its RSpec helpers into RSpec itself (no + # lib/view_component/rspec.rb in v4), so include them for type: :component specs. + require 'view_component/test_helpers' + config.include ViewComponent::TestHelpers, type: :component # Filter lines from Rails gems in backtraces. config.filter_rails_from_backtrace!