Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Clean up documentation leftovers for trigger_inclusion behaviour #2878

Merged
merged 1 commit into from
Mar 3, 2021

Conversation

pirj
Copy link
Member

@pirj pirj commented Mar 1, 2021

Fixes #2775
Related:

To sum it all up, the current behavior (apply_to_host_groups) is:

1.1. Configuration-level methods (include, extend, prepend, include_context, before, after, around) are tagged with metadata that is matched against example group metadata (and example metadata, as an example has a singleton example group), and are included. E.g.:

RSpec.configuration do |config|
  config.before(:example, litter_env: true) do
    ENV['litter'] = 'true'
  end
end

RSpec.describe do
  it 'survives litter flood', :litter_env do
    expect(ENV['litter']).to eq('true')
  end
end

1.2. Example group-level methods' (shared_examples/shared_examples_for, shared_context) metadata is applied to host groups that explicitly include them. E.g.:

RSpec.describe do
  shared_context 'in the wild', cold: true do
    let(:temp) { -30 }
  end

  context 'winter' do
    include_context 'in the wild'

    it 'is cold' do |example|
      expect(example.metadata[:cold]).to be(true)
      expect(temp).to eq(-30)
    end
  end
end

1.3. Example group-level methods' (shared_examples/shared_examples_for, shared_context) do not include shared groups to example groups based on matching metadata. E.g.:

RSpec.describe do
  shared_context 'done', done: true do
    before do
      ENV['done'] = 'done'
    end
  end

  it 'is done', :done do
    expect(ENV['done']).to be(nil)
  end
end

1.4 Example group-level hook methods are executed for examples with matching metadata. E.g.:

RSpec.describe do
  let(:pocket) { [] }

  before(:example, profit: true) do
    pocket << :penny
  end

  it 'is empty' do
    expect(pocket).to be_empty
  end

  it 'is full', :profit do
    expect(pocket).to contain_exactly(:penny)
  end
end

Just to avoid any confusion:

2.1. Configuration-level methods shared_context/shared_examples/shared_examples_for are not defined

2.2. Configuration-level methods include_examples/it_behaves_like are not defined

2.3. Example group-level include_context/include_examples/it_behaves_like (as opposed to configuration-level counterparts) do not accept metadata, but rather parameters that are passed to the included shared group:

RSpec.describe do
  shared_context 'foo' do |var:|
    let(:foo) { var }
  end

  include_context 'foo', var: :bar

  it 'foo is bar' do
    expect(foo).to eq(:bar)
  end
end

Quite obvious but confused me:

3.1. Included example groups override memoized helpers defined in the including example group:

RSpec.describe "open-close" do
  let(:open) { false }

  shared_context "is open" do
    let(:open) { true }
  end

  include_context "is open"

  it 'overrides open' do
    expect(open).to be(true)
  end
end

⚠️ It is also order-dependent, see this ticket.

@pirj pirj self-assigned this Mar 1, 2021
@pirj pirj added this to the 4.0 milestone Mar 1, 2021
@pirj pirj requested review from benoittgt and JonRowe March 1, 2021 21:54
@pirj pirj merged commit fa0a149 into 4-0-dev Mar 3, 2021
@pirj pirj deleted the clean-up-trigger_inclusion-related-docs branch March 3, 2021 20:26
@benoittgt
Copy link
Member

I love those short examples @pirj :)

yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 19, 2021
…rigger_inclusion-related-docs

Clean up documentation leftovers for trigger_inclusion behaviour

---
This commit was imported from rspec/rspec-core@fa0a149.
pirj added a commit to rubocop/rubocop-rspec that referenced this pull request Jul 9, 2022
Starting from RSpec 4, the implicit shared context inclusion, when a
shared context would have been included to an example if the example has
matching metadata, is not the case anymore.

See:
 - rspec/rspec-core#2834
 - rspec/rspec-core#2832
 - rspec/rspec-core#2878
pirj added a commit to rubocop/rubocop-rspec that referenced this pull request Jul 9, 2022
Starting from RSpec 4, the implicit shared context inclusion, when a
shared context would have been included to an example if the example has
matching metadata, is not the case anymore.

See:
 - rspec/rspec-core#2834
 - rspec/rspec-core#2832
 - rspec/rspec-core#2878
pirj added a commit to rubocop/rubocop-rspec that referenced this pull request Jul 9, 2022
Starting from RSpec 4, the implicit shared context inclusion, when a
shared context would have been included to an example if the example has
matching metadata, is not the case anymore.

See:
 - rspec/rspec-core#2834
 - rspec/rspec-core#2832
 - rspec/rspec-core#2878
pirj added a commit to rubocop/rubocop-rspec that referenced this pull request Jul 10, 2022
Starting from RSpec 4, the implicit shared context inclusion, when a
shared context would have been included to an example if the example has
matching metadata, is not the case anymore.

See:
 - rspec/rspec-core#2834
 - rspec/rspec-core#2832
 - rspec/rspec-core#2878
pirj added a commit to rubocop/rubocop-capybara that referenced this pull request Dec 29, 2022
Starting from RSpec 4, the implicit shared context inclusion, when a
shared context would have been included to an example if the example has
matching metadata, is not the case anymore.

See:
 - rspec/rspec-core#2834
 - rspec/rspec-core#2832
 - rspec/rspec-core#2878
ydah pushed a commit to rubocop/rubocop-factory_bot that referenced this pull request Apr 13, 2023
Starting from RSpec 4, the implicit shared context inclusion, when a
shared context would have been included to an example if the example has
matching metadata, is not the case anymore.

See:
 - rspec/rspec-core#2834
 - rspec/rspec-core#2832
 - rspec/rspec-core#2878
ydah pushed a commit to rubocop/rubocop-rspec_rails that referenced this pull request Mar 27, 2024
Starting from RSpec 4, the implicit shared context inclusion, when a
shared context would have been included to an example if the example has
matching metadata, is not the case anymore.

See:
 - rspec/rspec-core#2834
 - rspec/rspec-core#2832
 - rspec/rspec-core#2878
ydah pushed a commit to rubocop/rubocop-rspec_rails that referenced this pull request Mar 27, 2024
Starting from RSpec 4, the implicit shared context inclusion, when a
shared context would have been included to an example if the example has
matching metadata, is not the case anymore.

See:
 - rspec/rspec-core#2834
 - rspec/rspec-core#2832
 - rspec/rspec-core#2878
pirj added a commit to rubocop/rubocop-rspec_rails that referenced this pull request Aug 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants