Is it possible to check if a module is already included in RSpec configuration?

Viewed 69

In a gem, I define a spec helper module that needs to be included in RSpec config.

Initially, we decided to let the user require the module file and to include it explicitly in RSpec.configure block. Now we are going to change this behavior and automatically include our module:

# spec/support/my_mod.rb

module MyMod
  # ...
end

RSpec.configure do |config|
  config.include MyMod
end

Is there a way to check if our module is already included in RSpec's config? Checking Configuration docs, I don't see a method that could help here.

UPDATE: I have just realised that this is an uncommon scenario, because it's useless to check if the module is already included in the module file itself - the manual inclusion in the app happens later :(

1 Answers

A workaround solution. @include_modules contains an instance of RSpec::Core::FilterableItemRepository::QueryOptimized which has items_and_filters method.

RSpec.configure do |config|
  included_modules = RSpec.configuration.instance_variable_get(:@include_modules).items_and_filters.map(&:first)
  unless included_modules.include?(MyMod)
    config.include MyMod
  end
end
> RSpec.configuration.instance_variable_get(:@include_modules).items_and_filters
=> [[MyMod, {}]]
Related