diff --git a/CHANGELOG.md b/CHANGELOG.md index a99fad8e7..500c09eb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Master (Unreleased) * Fix an error for `RSpec/Capybara/SpecificFinders` with no parentheses. ([@ydah][]) +* Fix a false positive for `RSpec/NoExpectationExample` with pending using `skip` or `pending` inside an example. ([@ydah][]) ## 2.13.1 (2022-09-12) diff --git a/lib/rubocop/cop/rspec/no_expectation_example.rb b/lib/rubocop/cop/rspec/no_expectation_example.rb index f4689698d..7e8d0a868 100644 --- a/lib/rubocop/cop/rspec/no_expectation_example.rb +++ b/lib/rubocop/cop/rspec/no_expectation_example.rb @@ -49,10 +49,18 @@ class NoExpectationExample < Base send_pattern('#Expectations.all') ) + # @!method including_any_skip_example?(node) + # @param [RuboCop::AST::Node] node + # @return [Boolean] + def_node_search :including_any_skip_example?, <<~PATTERN + (send nil? {:pending :skip} ...) + PATTERN + # @param [RuboCop::AST::BlockNode] node def on_block(node) return unless regular_or_focused_example?(node) return if including_any_expectation?(node) + return if including_any_skip_example?(node) add_offense(node) end diff --git a/spec/rubocop/cop/rspec/no_expectation_example_spec.rb b/spec/rubocop/cop/rspec/no_expectation_example_spec.rb index 8eac4c8dd..265f46b1c 100644 --- a/spec/rubocop/cop/rspec/no_expectation_example_spec.rb +++ b/spec/rubocop/cop/rspec/no_expectation_example_spec.rb @@ -115,6 +115,28 @@ end end + context 'with no expectation pending example when using `pending` ' \ + 'inside an example' do + it 'registers no offenses' do + expect_no_offenses(<<~RUBY) + it "is implemented but waiting" do + pending "something else getting finished" + end + RUBY + end + end + + context 'with no expectation skipped example when using `skip` ' \ + 'inside an example' do + it 'registers no offenses' do + expect_no_offenses(<<~RUBY) + it "is skipped" do + skip + end + RUBY + end + end + context 'when Ruby 2.7', :ruby27 do context 'with no expectation example with it' do it 'registers an offense' do