Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Changing opal-rspec directory (useful if you cannot upgrade to opal-rails > 7.0)

opal-rspec does not currently have a means to change the spec directory location, which you really need to do if you are going to have mixed opal and regular (server) ruby code testing.

The following assumes normal specs will go into the the spec directory, and opal specs will go into spec-opal directory.

To get this to work, you have to change the Opal::Server path to end with spec-opal instead of spec which is what opal-rspec normally does. Then you have to change the Opal::Server main file (the sprockets_runner.rb.erb) file to be a copy which pulls in your specs.

In your rake file do this:

require 'opal/rspec/rake_task'
require 'bundler'
Bundler.require

Opal::RSpec::RakeTask.new(:spec_opal) do |s|
  # replace the last sprocket path (i.e. 'spec') with 'spec-opal'
  s.sprockets.paths.tap { s.sprockets.clear_paths }[0..-2].each { |path| s.sprockets.append_path path}
  s.append_path 'spec-opal'
  # replace the main runner with you own runner which can be placed in spec-opal/sprockets_runner.rb.erb
  s.main = 'sprockets_runner'

end

create your own sprockets_runner file by adding this file named sprockets_runner.rb.erb to the spec-opal directory:

#spec-opal/sprockets_runner.rb.erb
# This file is used by Opal::Server to basically load all spec files that
# can be found in the spec-opal/ directory. 

require 'opal'
require 'opal-rspec'

<% Dir.glob('spec-opal/**/*_spec.{rb,opal}').each do |s| %>
require <%= s.sub(/^spec-opal\//, '').sub(/\.(rb|opal)$/, '').inspect %>
<% end %>

Opal::RSpec::Runner.autorun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.