Excluding lines from the backtrace

To reduce the noise when diagnosing failures, RSpec can exclude lines belonging to certain gems or matching given patterns.

If you want to filter out backtrace lines belonging to specific gems, you can use config.filter_gems_from_backtrace like so:

  config.filter_gems_from_backtrace "ignored_gem", "another_ignored_gem",

For more control over which lines to ignore, you can use the the backtrace_exclusion_patterns option to either replace the default exclusion patterns, or append your own, e.g.

  config.backtrace_exclusion_patterns = [/first pattern/, /second pattern/]
  config.backtrace_exclusion_patterns << /another pattern/

The default exclusion patterns are:

  /\/lib\d*\/ruby\//,
  /org\/jruby\//,
  /bin\//,
  /lib\/rspec\/(core|expectations|matchers|mocks)/

Additionally, rspec can be run with the --backtrace option to skip backtrace cleaning entirely.

Using default backtrace_exclusion_patterns

Given a file named “spec/failing_spec.rb” with:

RSpec.describe "2 + 2" do
  it "is 5" do
    expect(2+2).to eq(5)
  end
end

When I run rspec

Then the output should contain “1 example, 1 failure”

And the output should not contain “lib/rspec/expectations”.

Replacing backtrace_exclusion_patterns

Given a file named “spec/spec_helper.rb” with:

RSpec.configure do |config|
  config.backtrace_exclusion_patterns = [
    /spec_helper/
  ]
end

And a file named “spec/example_spec.rb” with:

require 'spec_helper'
RSpec.describe "foo" do
  it "returns baz" do
    expect("foo").to eq("baz")
  end
end

When I run rspec

Then the output should contain “1 example, 1 failure”

And the output should contain “lib/rspec/expectations”.

Appending to backtrace_exclusion_patterns

Given a file named “spec/support/assert_baz.rb” with:

require "support/really_assert_baz"

def assert_baz(arg)
  really_assert_baz(arg)
end

And a file named “spec/support/reallyassertbaz.rb” with:

def really_assert_baz(arg)
  expect(arg).to eq("baz")
end

And a file named “spec/example_spec.rb” with:

require "support/assert_baz"
RSpec.configure do |config|
  config.backtrace_exclusion_patterns << /really/
end

RSpec.describe "bar" do
  it "is baz" do
    assert_baz("bar")
  end
end

When I run rspec

Then the output should contain “1 example, 1 failure”

And the output should contain “assert_baz”

But the output should not contain “reallyassertbaz”

And the output should not contain “lib/rspec/expectations”.

Running rspec with --backtrace prints unfiltered backtraces

Given a file named “spec/support/custom_helper.rb” with:

def assert_baz(arg)
  expect(arg).to eq("baz")
end

And a file named “spec/example_spec.rb” with:

require "support/custom_helper"

RSpec.configure do |config|
  config.backtrace_exclusion_patterns << /custom_helper/
end

RSpec.describe "bar" do
  it "is baz" do
    assert_baz("bar")
  end
end

When I run rspec --backtrace

Then the output should contain “1 example, 1 failure”

And the output should contain “spec/support/customhelper.rb:2:in `assertbaz’”

And the output should contain “lib/rspec/expectations”

And the output should contain “lib/rspec/core”.

Using filter_gems_from_backtrace to filter the named gem

Given a vendored gem named “mygem” containing a file named “lib/mygem.rb” with:

class MyGem
  def self.do_amazing_things!
    # intentional bug to trigger an exception
    impossible_math = 10 / 0
    "10 div 0 is: #{impossible_math}"
  end
end

And a file named “spec/usemygem_spec.rb” with:

require 'my_gem'

RSpec.describe "Using my_gem" do
  it 'does amazing things' do
    expect(MyGem.do_amazing_things!).to include("10 div 0 is")
  end
end

And a file named “spec/spec_helper.rb” with:

RSpec.configure do |config|
  config.filter_gems_from_backtrace "my_gem"
end

Then the output from rspec should contain “vendor/mygem-1.2.3/lib/mygem.rb:4:in `doamazingthings!‘”

But the output from rspec --require spec_helper should not contain “vendor/mygem-1.2.3/lib/mygem.rb:4:in `doamazingthings!’”.