opal-rspec allows opal to use rspec for running specs in javascript environments. It comes with built-in support for running rspec with custom phantomjs and standard web browser formatters. Also, async spec examples are supported to reflect browser usage of ruby applications.

describe User do
  it "can be created with a name" do
    expect( be_persisted


Add the opal-rspec gem to your Gemfile:

# Gemfile
gem 'opal'
gem 'opal-rspec'

Running specs


To run specs, a rake task can be added which will load all spec files from spec/:

# Rakefile
require 'opal/rspec/rake_task'

Then, to run your specs inside phantomjs, just run the rake task:

$ bundle exec rake

In a Browser

opal-rspec can use sprockets to build and serve specs over a simple rack server. Add the following to a file:

require 'bundler'

run { |s|
  s.main = 'opal/rspec/sprockets_runner'
  s.append_path 'spec'
  s.debug = false

Then run the rack server bundle exec rackup and visit http://localhost:9292 in any web browser.

Async examples

opal-rspec adds support for async specs to rspec. These specs are defined using #async instead of #it:

describe MyClass do
  # normal example
  it 'does something' do
    expect(:foo).to eq(:foo)

  # async example
  async 'does something else, too' do
    # ...

This just marks the example as running async. To actually handle the async result, you also need to use a run_async call inside some future handler:

async 'HTTP requests should work' do
  HTTP.get('/users/1.json') do |res|
    run_async {
      expect(res).to be_ok

The block passed to run_async informs the runner that this spec is finished so it can move on. Any failures/expectations run inside this block will be run in the context of the example.