Skip to content

Instantly share code, notes, and snippets.

@Rodrigora
Created February 7, 2024 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rodrigora/21082ee84d448a2fc245536d2e773c64 to your computer and use it in GitHub Desktop.
Save Rodrigora/21082ee84d448a2fc245536d2e773c64 to your computer and use it in GitHub Desktop.
Stimulus/Capybara helper to ensure that the stimulus application loaded all controllers
module StimulusHelper
CouldNotLoadStimulus = Class.new(StandardError) do
def initialize(missing_controllers)
super("Could not load all controllers. missing: #{missing_controllers}")
end
end
# Reads the list of controllers from the filesystem
APPLICATION_CONTROLLERS = Dir[Rails.root.join('app/javascript/controllers/*')].map do |path|
File.basename(path).gsub('_controller.js', '').gsub('_', '-')
end - ['application.js', 'index.js']
def ensure_stimulus_is_ready
tries = 0
begin
missing_controllers = APPLICATION_CONTROLLERS - loaded_controllers
raise CouldNotLoadStimulus, missing_controllers if missing_controllers.any?
rescue CouldNotLoadStimulus => e
tries += 1
raise e if tries > 3
sleep 0.2
retry
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment