Infer type of view component specs from file location [rspec-rails]

92 views
Skip to first unread message

Marcelo Lauxen

unread,
Oct 16, 2020, 3:45:43 AM10/16/20
to rspec
Hello guys, I was ready to open a feature request, but I'm not sure if it would make sense for rspec-rails so I'm asking about this here.

This gem github/view_component allow us build components for rails, and their convention is put specs on "spec/components", we need add TestHelpers github/view_component#setting-up-rspec on the configuration for specs of :component type and thats fine, but the only think which is frustating me is the fact that rspec-rails isn't able to infer the type of the spec based on the file location, because the directory mappings doesn't include `spec/components` rspec/rails/configuration.rb#L28 and at our projects we use the infer_spec_type_from_file_location! to avoid the needing of set the spec type, but only for view_components we need define the type.

In theory only add "component:  %w[spec components]" to the directory mappings would be enough, but because view_component isn't a rails feature I'm not sure if this would make sense to the project and maybe the best approach would be set the type of the spec on each file for this cases.

What is your opinion on this? I'm a newbie on ruby/rails/rspec stack and maybe I'm saying a lot of bullshit here (my English is not the best, too), sorry for anything.

 


Jon Rowe

unread,
Oct 16, 2020, 3:56:50 AM10/16/20
to rs...@googlegroups.com
Hi Marcelo

Thanks for bringing this to the mailing list, unfortunately thats something we wouldn’t be interested in adding to rspec-rails, for a couple of reasons.

The main reason being we don’t support 3rd party libraries other than Rails own dependencies directly within rspec-rails, given the size of the rails eco system this would lead to massive feature bloat on our part, we prefer to provide the tools for other gems to hook into us, even if it’s just with a `rspec-view_component` style gem etc.

We also consider `infer_spec_type_from_file_location!` to be a legacy feature for older rails test suites so don’t want to encourage further adoption (we’d like to deprecate it and remove it one day).

However you can add the type yourself to get the functionality you want, rspec-rails uses the public rspec api’s to do this like so:

    RSpec.configure do |c|
      c.define_derived_metadata(file_path:  /spec[\\\/]components[\\\/]/) do |metadata|
        metadata[:type] ||= :component
      end
    end

This would be added to your spec helper and should allow you to not have to declare the component type manually, you could even suggest it to the view_component authors as part of any test helpers they have.

Marcelo Lauxen

unread,
Oct 16, 2020, 8:04:08 AM10/16/20
to rs...@googlegroups.com
Thanks for the explanation Jon!

And thanks for the workaround solution, that will be enough! 



--
You received this message because you are subscribed to a topic in the Google Groups "rspec" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rspec/clOruGlpuMs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rspec+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/dejalu-217-5562a0e3-bb7d-4115-b9b8-8c9a595359af%40jonrowe.co.uk.
Reply all
Reply to author
Forward
0 new messages