rspec / rspec-core

RSpec runner and formatters
http://rspec.info
MIT License
1.23k stars 765 forks source link

Filters on `include_context` are ignored #2941

Closed zog closed 2 years ago

zog commented 2 years ago

Subject of the issue

Filters on include_context are ignored if passed in the wrong order

Your environment

Steps to reproduce

# frozen_string_literal: true

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  gem "rspec", "3.11.0" # Activate the gem and version you are reporting the issue against.
end

puts "Ruby version is: #{RUBY_VERSION}"
require 'rspec/autorun'

RSpec.shared_context 'default context' do
  let(:value) { 1 }
end

RSpec.shared_context 'my context' do
  let(:value) { 2 }
end

RSpec.configure do |config|
  config.include_context 'default context', type: :bug_report
  config.include_context 'my context', :filter_me, type: :bug_report
end

RSpec.describe 'include filters', type: :bug_report do
  it 'should have value == 1' do
    expect(value).to eq 1
  end

  it 'should have value == 2', :filter_me do
    expect(value).to eq 2
  end
end

Expected behavior

The context should only be included on examples matching the filters

Actual behavior

The context is always included

Workaround

Inverting the filters order fixes the issue:

# frozen_string_literal: true

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  gem "rspec", "3.11.0" # Activate the gem and version you are reporting the issue against.
end

puts "Ruby version is: #{RUBY_VERSION}"
require 'rspec/autorun'

RSpec.shared_context 'default context' do
  let(:value) { 1 }
end

RSpec.shared_context 'my context' do
  let(:value) { 2 }
end

RSpec.configure do |config|
  config.include_context 'default context', type: :bug_report
  config.include_context 'my context', { type: :bug_report }, :filter_me
end

RSpec.describe 'include filters', type: :bug_report do
  it 'should have value == 1' do
    expect(value).to eq 1
  end

  it 'should have value == 2', :filter_me do
    expect(value).to eq 2
  end
end
pirj commented 2 years ago

This seems to be a duplicate of #1821, that is fixed in #2878, and will be released in RSpec 4. I encourage you to use the 4-0-dev branch if you want to use it before the release. Can you please confirm that 4-0-dev fixes the issue?

JonRowe commented 2 years ago

I tested this on 4-0-dev and it was fixed (using the snippet above) closing for now but please re open if you find an issue against 4-0-dev