Does :focus override :slow when both tags are present in RSpec 3.12?
0 reputation · 11 Aug 2022, 13:25 UTC
0 reputation · 11 Aug 2022, 13:25 UTC
Goal: Determine whether RSpec’s filter_run_when_matching :focus takes precedence over filter_run_excluding :slow when an example is tagged with both :focus and :slow in RSpec 3.12.
Constraints: The official documentation does not specify the precedence between these two filter types, and anecdotal reports show inconsistent behavior across different patch levels. Additionally, command‑line options such as --tag ~slow may override or interact with the internal filters in an undocumented way.
Questions: Which filter wins when both are active? Does the order in which filter_run_when_matching and filter_run_excluding are configured affect the result? How does rspec --tag ~slow combine with the internal focus and slow filters?
35275 reputation · 11 Aug 2022, 20:06 UTC
No. In RSpec 3.12 (and all 3.x releases), an example tagged with both :focus and :slow will be excluded when filter_run_when_matching :focus and filter_run_excluding :slow are both active. Exclusion filters take precedence over inclusion filters regardless of configuration order or CLI flags.
RSpec merges all inclusion filters (from filter_run_when_matching, filter_run_including, and --tag focus) into a single inclusion set, and all exclusion filters (from filter_run_excluding and --tag ~slow) into a single exclusion set. The engine then applies the logic: run examples that match the inclusion set and do not match the exclusion set. Because :slow is in the exclusion set, any example carrying that tag is removed even if it also carries :focus.
:slow) always wins.filter_run_when_matching :focus before or after filter_run_excluding :slow produces the same result.rspec --tag ~slow combine? It adds another exclusion filter. The union of configured and CLI exclusions is applied, so --tag ~slow also excludes the :focus, :slow example.# spec/filter_precedence_spec.rb
RSpec.describe 'Filter precedence' do
it 'runs when only focus', :focus do
expect(true).to be true
end
it 'is excluded despite focus', :focus, :slow do
expect(true).to be true
end
end
spec_helper.rb (or rails_helper.rb):RSpec.configure do |config|
config.filter_run_when_matching :focus
config.filter_run_excluding :slow
end
bundle exec rspec. Only the first example executes; the second is excluded.bundle exec rspec --tag ~slow (remove the configured exclusion). Same result — the :focus, :slow example is still excluded.:focus to force-run a slow test:slow tag from that specific example.bundle exec rspec --tag ~slow:false is not supported; instead, comment out filter_run_excluding :slow or run with a custom profile that omits it.:focus first (advanced, rarely needed).Are you using the deprecated filter_run :focus (without _when_matching) anywhere in your configuration? That API behaves differently and can produce the inconsistent results mentioned in anecdotal reports.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.