Read more

Ensure passing Jasmine specs from your Ruby E2E tests

Henning Koch
October 13, 2021Software engineer at makandra GmbH

Jasmine Show archive.org snapshot is a great way to unit test your JavaScript components without writing an expensive end-to-end test for every small requirement.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

After we integrated Jasmine into a Rails app we often add an E2E test that opens that Jasmine runner and expects all specs to pass. This way we see Jasmine failures in our regular test runs.

RSpec

In a feature spec Show archive.org snapshot you can run Jasmine specs like this:

scenario 'Jasmine tests' do
  visit test_jasmine_path
  expect(page).to have_content('finished in') # wait for all tests to finish
  expect(page).not_to have_css('.jasmine-failed .jasmine-description') # better error messages
  expect(page).to have_css('.jasmine-bar.jasmine-passed') # safer check
end

Cucumber

In Cucumber you can run Jasmine specs like this:

Scenario: Jasmine tests
  When I run Jasmine tests
  Then all specs should have passed
When 'I run Jasmine tests' do
  visit test_jasmine_path
end

Then 'all specs should have passed' do
  expect(page).to have_css('.jasmine-version'), 'Failed to load Jasmine page'
  using_wait_time 60 do
    expect(page).to have_content('finished in') # patiently wait for all tests to finish
  end

  # The above check will wait until tests are finished; once they are, we want
  # to show fails early
  using_wait_time 0 do
    failed_specs = page.all('.jasmine-failed .jasmine-description').map(&:text)
    puts 'Failed Jasmine specs:' if failed_specs.any?
    failed_specs.each do |description|
      puts "* #{description}"
    end
    expect(failed_specs.size).to eq(0), "Some Jasmine Specs failed (see above)"

    expect(page).to have_css('.jasmine-bar.jasmine-passed') # safer check
  end
end
Posted by Henning Koch to makandra dev (2021-10-13 13:30)