From 6271a27e28b6925532196fc500aa6f0c05b93c78 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sun, 12 Jan 2020 21:08:45 +0000 Subject: [PATCH] [core] Merge pull request rspec/rspec-core#2687 from rspec/add-around-context-warning Emit a warning for `around(:context)` --- This commit was imported from https://github.com/rspec/rspec-core/commit/36c5ad5a061fb9b5cd1c9aafe423079209fdc2b5. --- rspec-core/Changelog.md | 8 +++++++ rspec-core/lib/rspec/core/hooks.rb | 5 ++++ rspec-core/spec/rspec/core/hooks_spec.rb | 29 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/rspec-core/Changelog.md b/rspec-core/Changelog.md index 40c1237da..1598f0e4a 100644 --- a/rspec-core/Changelog.md +++ b/rspec-core/Changelog.md @@ -1,3 +1,11 @@ +### Development +[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.9.1...master) + +Bug Fixes: + +* Emit a warning when `around` hook is used with `:context` scope + (Phil Pirozhkov, #2687) + ### 3.9.1 / 2019-12-28 [Full Changelog](http://github.com/rspec/rspec-core/compare/v3.9.0...v3.9.1) diff --git a/rspec-core/lib/rspec/core/hooks.rb b/rspec-core/lib/rspec/core/hooks.rb index 56c0be852..6cde2ca1a 100644 --- a/rspec-core/lib/rspec/core/hooks.rb +++ b/rspec-core/lib/rspec/core/hooks.rb @@ -448,6 +448,11 @@ def register(prepend_or_append, position, *args, &block) "`#{position}(:suite)` hook, registered on an example " \ "group, will be ignored." return + elsif scope == :context && position == :around + # TODO: consider making this an error in RSpec 4. For SemVer reasons, + # we are only warning in RSpec 3. + RSpec.warn_with "WARNING: `around(:context)` hooks are not supported and " \ + "behave like `around(:example)." end hook = HOOK_TYPES[position][scope].new(block, options) diff --git a/rspec-core/spec/rspec/core/hooks_spec.rb b/rspec-core/spec/rspec/core/hooks_spec.rb index d9fb62de9..895de436c 100644 --- a/rspec-core/spec/rspec/core/hooks_spec.rb +++ b/rspec-core/spec/rspec/core/hooks_spec.rb @@ -492,5 +492,34 @@ def yielder :hooks ]) end + + it 'emits a warning for `around(:context)`' do + expect(RSpec).to receive(:warn_with).with(a_string_including( + '`around(:context)` hooks are not supported' + )) + RSpec.describe do + around(:context) { } + end + end + + it 'emits a warning for `around(:context)` defined in `configure`' do + expect(RSpec).to receive(:warn_with).with(a_string_including( + '`around(:context)` hooks are not supported' + )) + RSpec.configure do |c| + c.around(:context) { } + end + end + + [:before, :around, :after].each do |type| + it "emits a warning for `#{type}(:suite)` hooks" do + expect(RSpec).to receive(:warn_with).with(a_string_including( + "`#{type}(:suite)` hooks are only supported on the RSpec configuration object." + )) + RSpec.describe do + send(type, :suite) { } + end + end + end end end