Skip to content

RSpec can't match errors in jruby-9.4.0.0 #7112

@pboling

Description

@pboling

Environment Information

Provide at least:

❯ jruby -v
jruby 9.4.0.0-SNAPSHOT (3.1.0) 2022-02-08 3562498c80 Java HotSpot(TM) 64-Bit Server VM 13.0.2+8 on 13.0.2+8 +jit [darwin-x86_64]
❯ uname -a
Darwin SpaceBar.local 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:41 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T6000 arm64
❯ gem -v
3.3.7
❯ bundler -v
Bundler version 2.3.7

Other relevant info you may wish to add:

Expected Behavior

RSpec and MiniTest can both match errors that are raised. In all other tested versions of Ruby, JRuby, TruffleRuby this works.

Actual Behavior

Only MiniTest can match the errors. RSpec cannot. It does not matter if the matching is on string equality or regex pattern matching.

GREEN TEST - MiniTest POC

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'hashie'
  gem 'minitest'
end

puts "RUBY_VERSION: #{RUBY_VERSION} - RUBY_ENGINE: #{RUBY_ENGINE} - JRUBY_VERSION: #{JRUBY_VERSION}"

class StrictKeyAccessHash < Hash
  include Hashie::Extensions::StrictKeyAccess # Raise KeyError on failed key access
end

require 'minitest/autorun'

class TestCalculator < Minitest::Test
  def setup
    @hash = StrictKeyAccessHash[foo: "bar"]
  end

  def test_success
    assert_equal "bar", @hash[:foo]
  end

  def test_equality
    exception = assert_raises KeyError do
      @hash["oops"]
    end
    assert_equal("key not found: \"oops\"", exception.message)
  end

  def test_matching
    exception = assert_raises KeyError do
      @hash["flare"]
    end
    assert_match(/flare/, exception.message)
  end
end

Output from MiniTest

❯ jruby jruby_minitest_key_error_match_working.rb
RUBY_VERSION: 3.1.0 - RUBY_ENGINE: jruby - JRUBY_VERSION: 9.4.0.0-SNAPSHOT
Run options: --seed 30060

# Running:

...

Finished in 0.070110s, 42.7900 runs/s, 85.5800 assertions/s.

3 runs, 6 assertions, 0 failures, 0 errors, 0 skips
jruby jruby_minitest_key_error_match_working.rb  14.45s user 1.79s system 201% cpu 8.046 total

RED TEST - RSpec POC

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'hashie'
  gem 'rspec'
end

puts "RUBY_VERSION: #{RUBY_VERSION} - RUBY_ENGINE: #{RUBY_ENGINE} - JRUBY_VERSION: #{JRUBY_VERSION}"

class StrictKeyAccessHash < Hash
  include Hashie::Extensions::StrictKeyAccess # Raise KeyError on failed key access
end

require 'rspec/autorun'

RSpec.describe StrictKeyAccessHash do
  before do
    @hash = StrictKeyAccessHash[foo: "bar"]
  end

  it "test_success" do
    expect("bar").to eq @hash[:foo]
  end

  it "matches exact" do
    expect {
      @hash["oops"]
    }.to raise_error(KeyError, "key not found: \"oops\"")
  end

  it "matches regex" do
    expect {
      @hash["flare"]
    }.to raise_error(KeyError, /flare/)
  end
end

Output from RSpec

❯ jruby jruby_rspec_key_error_match_not_working.rb
RUBY_VERSION: 3.1.0 - RUBY_ENGINE: jruby - JRUBY_VERSION: 9.4.0.0-SNAPSHOT
.FF

Failures:

  1) StrictKeyAccessHash matches exact
     Failure/Error:
       expect {
         @hash["oops"]
       }.to raise_error(KeyError, "key not found: \"oops\"")

       expected KeyError with "key not found: \"oops\"", got #<KeyError: key not found: "oops"> with backtrace:
         # jruby_rspec_key_error_match_not_working.rb:28:in `block in <main>'
         # jruby_rspec_key_error_match_not_working.rb:29:in `block in <main>'
     # jruby_rspec_key_error_match_not_working.rb:29:in `block in <main>'

  2) StrictKeyAccessHash matches regex
     Failure/Error:
       expect {
         @hash["flare"]
       }.to raise_error(KeyError, /flare/)

       expected KeyError with message matching /flare/, got #<KeyError: key not found: "flare"> with backtrace:
         # jruby_rspec_key_error_match_not_working.rb:34:in `block in <main>'
         # jruby_rspec_key_error_match_not_working.rb:35:in `block in <main>'
     # jruby_rspec_key_error_match_not_working.rb:35:in `block in <main>'

Finished in 0.21987 seconds (files took 0.73576 seconds to load)
3 examples, 2 failures

Failed examples:

rspec jruby_rspec_key_error_match_not_working.rb:26 # StrictKeyAccessHash matches exact
rspec jruby_rspec_key_error_match_not_working.rb:32 # StrictKeyAccessHash matches regex

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions