guard-jruby-rspec
guard-jruby-rspec copied to clipboard
Ability to add custom reloaders to run before running all specs.
This ability will give us resolve issues related to missing constants, loading/requiring files.
Hi @sumitmah, thanks for this! Before we get this ready to merge, could you help us understand the scenario in which you want to have special reloaders associated with run_all but not normal runs?
@nilbus As it is mentioned in readme,
Since JRuby cannot fork, guard-jruby-rspec reloads code with load on each changed path, which can potentially cause weird side side effects or errors.
I was also facing similar issues with my Rails project. e.g. for shared specs(I saw similar issue already posted for this gem) "context not found". This was happening because of following code.
def unload_previous_examples
::RSpec.configuration.reset
::RSpec.world.reset
end
::RSpec.world.reset will remove shared example groups. Following is definition of reset method. I had to reload these examples explicitly in the custom reloader for run all on rerun. Since following method clears the example groups.
def reset
example_groups.clear
@shared_example_group_registry = nil
end
Here is example from my guardfile
reload_shared_specs = lambda do
if Object.const_defined?('Rails')
$LOADED_FEATURES.reject! { |path| path =~ /\/spec\/support\//}
ActiveSupport::Dependencies.loaded.reject! { |path| path =~ /\/spec\/support\//}.inspect
ActiveSupport::Dependencies.autoloaded_constants.each {|c| ActiveSupport::Dependencies.loaded.reject! { |path| path =~ /#{c.to_s.underscore}/} }
ActiveSupport::Dependencies.autoloaded_constants.each {|c| $LOADED_FEATURES.reject! { |path| path =~ /#{c.to_s.underscore}/} }
ActiveSupport::Dependencies.explicitly_unloadable_constants.each {|c| ActiveSupport::Dependencies.loaded.reject! { |path| path =~ /#{c.to_s.underscore}/} }
ActiveSupport::Dependencies.explicitly_unloadable_constants.each {|c| $LOADED_FEATURES.reject! { |path| path =~ /#{c.to_s.underscore}/} }
Dir[::Rails.root.join('spec/support/**/*.rb')].each { |f| ::RSpec.world.shared_example_group_registry.require_or_load(f) }
end
end
@nilbus @jkutner any thoughts on this?
Thanks for the background information, and sorry for the delay @sumitmah. I'm trying to connect the code changes with that background story. Is the Guardfile code you provided what you're running inside a custom_reloaders_for_run_all block?
@nilbus Yes