Skip to content

Fix leak of execution context between specs #2711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/rspec/rails/example/rails_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# suite and ammeter.
require 'rspec/rails/matchers'

if ::Rails::VERSION::MAJOR >= 7
require 'active_support/execution_context/test_helper'
end

module RSpec
module Rails
# @api public
Expand All @@ -12,7 +16,10 @@ module RailsExampleGroup
include RSpec::Rails::MinitestLifecycleAdapter
include RSpec::Rails::MinitestAssertionAdapter
include RSpec::Rails::FixtureSupport
include RSpec::Rails::TaggedLoggingAdapter if ::Rails::VERSION::MAJOR >= 7
if ::Rails::VERSION::MAJOR >= 7
include RSpec::Rails::TaggedLoggingAdapter
include ActiveSupport::ExecutionContext::TestHelper
end
end
end
end
27 changes: 27 additions & 0 deletions spec/rspec/rails/example/rails_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,32 @@ module RSpec::Rails
expect(described_class.private_instance_methods).to include(:tagged_logger)
end
end

it 'does not leak context between example groups', if: ::Rails::VERSION::MAJOR >= 7 do
groups =
[
RSpec::Core::ExampleGroup.describe("A group") do
include RSpec::Rails::RailsExampleGroup
specify { expect(ActiveSupport::ExecutionContext.to_h).to eq({}) }
end,
RSpec::Core::ExampleGroup.describe("A controller group", type: :controller) do
specify do
Rails.error.set_context(foo: "bar")
expect(ActiveSupport::ExecutionContext.to_h).to eq(foo: "bar")
end
end,
RSpec::Core::ExampleGroup.describe("Another group") do
include RSpec::Rails::RailsExampleGroup
specify { expect(ActiveSupport::ExecutionContext.to_h).to eq({}) }
end
]

results =
groups.map do |group|
group.run(failure_reporter) ? true : failure_reporter.exceptions
end

expect(results).to all be true
end
end
end