rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.14k stars 1.03k forks source link

Only load example group modules when they're used #2742

Open eugeneius opened 3 months ago

eugeneius commented 3 months ago

Currently the example group modules for all spec types are always loaded, regardless of which type of spec is being run. Some of those modules force parts of the user's application to load; for example, RSpec::Rails::HelperExampleGroup includes ActionView::TestCase::Behavior, and loading ActionView::TestCase loads all of the application's helpers.

By autoloading these modules and including them lazily the first time the corresponding spec type is defined, we can avoid loading parts of the user's application unnecessarily.

Co-authored by @ilianah 🍐

JonRowe commented 3 months ago

Anything we can do to load things only when needed gets a plus from me, however I'm slightly concerned the build is failing because our snippets are exposing the change in whats loaded by default (in the case of 3.2 its just minitest being missing) which makes me wonder if this is a breaking change from a users perspective but I'm willing to be convinced its not if its easy to fix the build.

The suggested improvement to rspec-core is also fine, I think the helpers you are using were actually invented for rspec-rails ironically (for the define derived types options)

eugeneius commented 3 months ago

Previously ActiveSupport::TestCase was always loaded, so minitest would be required here: https://github.com/rails/rails/blob/v7.1.3.2/activesupport/lib/active_support/test_case.rb#L3

The most straightforward fix for that is to continue to load minitest by requiring it explicitly; I've pushed a commit to do that.