1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
Feature: 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:
```ruby
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.
```ruby
config.backtrace_exclusion_patterns = [/first pattern/, /second pattern/]
config.backtrace_exclusion_patterns << /another pattern/
```
The default exclusion patterns are:
```ruby
/\/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.
Scenario: Using default `backtrace_exclusion_patterns`
Given a file named "spec/failing_spec.rb" with:
"""ruby
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"
Scenario: Replacing `backtrace_exclusion_patterns`
Given a file named "spec/spec_helper.rb" with:
"""ruby
RSpec.configure do |config|
config.backtrace_exclusion_patterns = [
/spec_helper/
]
end
"""
And a file named "spec/example_spec.rb" with:
"""ruby
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"
Scenario: Appending to `backtrace_exclusion_patterns`
Given a file named "spec/support/assert_baz.rb" with:
"""ruby
require "support/really_assert_baz"
def assert_baz(arg)
really_assert_baz(arg)
end
"""
And a file named "spec/support/really_assert_baz.rb" with:
"""ruby
def really_assert_baz(arg)
expect(arg).to eq("baz")
end
"""
And a file named "spec/example_spec.rb" with:
"""ruby
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 "really_assert_baz"
And the output should not contain "lib/rspec/expectations"
Scenario: Running `rspec` with `--backtrace` prints unfiltered backtraces
Given a file named "spec/support/custom_helper.rb" with:
"""ruby
def assert_baz(arg)
expect(arg).to eq("baz")
end
"""
And a file named "spec/example_spec.rb" with:
"""ruby
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/custom_helper.rb:2:in `assert_baz'"
And the output should contain "lib/rspec/expectations"
And the output should contain "lib/rspec/core"
Scenario: Using `filter_gems_from_backtrace` to filter the named gem
Given a vendored gem named "my_gem" containing a file named "lib/my_gem.rb" with:
"""ruby
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/use_my_gem_spec.rb" with:
"""ruby
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:
"""ruby
RSpec.configure do |config|
config.filter_gems_from_backtrace "my_gem"
end
"""
Then the output from `rspec` should contain "vendor/my_gem-1.2.3/lib/my_gem.rb:4:in `do_amazing_things!'"
But the output from `rspec --require spec_helper` should not contain "vendor/my_gem-1.2.3/lib/my_gem.rb:4:in `do_amazing_things!'"
|