diff --git a/config/default.yml b/config/default.yml index 7ed394d9e..777d9e82c 100644 --- a/config/default.yml +++ b/config/default.yml @@ -298,8 +298,15 @@ RSpec/DescribedClassModuleWrapping: RSpec/Dialect: Description: Enforces custom RSpec dialects. Enabled: false - PreferredMethods: {} + PreferredMethods: + background: before + scenario: it + xscenario: xit + given: let + given!: let! + feature: describe VersionAdded: '1.33' + VersionChanged: "<>" Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Dialect RSpec/DuplicatedMetadata: @@ -1018,14 +1025,6 @@ RSpec/Capybara/CurrentPathExpectation: VersionChanged: '2.0' Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Capybara/CurrentPathExpectation -RSpec/Capybara/FeatureMethods: - Description: Checks for consistent method usage in feature specs. - Enabled: true - EnabledMethods: [] - VersionAdded: '1.17' - VersionChanged: '2.0' - Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Capybara/FeatureMethods - RSpec/Capybara/MatchStyle: Description: Checks for usage of deprecated style methods. Enabled: pending diff --git a/config/obsoletion.yml b/config/obsoletion.yml index 1c4c0f8b8..9c560f12a 100644 --- a/config/obsoletion.yml +++ b/config/obsoletion.yml @@ -34,3 +34,9 @@ renamed: RSpec/Rails/MinitestAssertions: RSpecRails/MinitestAssertions RSpec/Rails/NegationBeValid: RSpecRails/NegationBeValid RSpec/Rails/TravelAround: RSpecRails/TravelAround + +removed: + RSpec/Capybara/FeatureMethods: + reason: > + this cop has migrated to `RSpec/Dialect`. Please use `RSpec/Dialect` instead. + in .rubocop.yml file, enable `RSpec/Dialect`. diff --git a/lib/rubocop/cop/rspec/capybara/feature_methods.rb b/lib/rubocop/cop/rspec/capybara/feature_methods.rb deleted file mode 100644 index 2b95dc70b..000000000 --- a/lib/rubocop/cop/rspec/capybara/feature_methods.rb +++ /dev/null @@ -1,104 +0,0 @@ -# frozen_string_literal: true - -module RuboCop - module Cop - module RSpec - module Capybara - # Checks for consistent method usage in feature specs. - # - # By default, the cop disables all Capybara-specific methods that have - # the same native RSpec method (e.g. are just aliases). Some teams - # however may prefer using some of the Capybara methods (like `feature`) - # to make it obvious that the test uses Capybara, while still disable - # the rest of the methods, like `given` (alias for `let`), `background` - # (alias for `before`), etc. You can configure which of the methods to - # be enabled by using the EnabledMethods configuration option. - # - # @example - # # bad - # feature 'User logs in' do - # given(:user) { User.new } - # - # background do - # visit new_session_path - # end - # - # scenario 'with OAuth' do - # # ... - # end - # end - # - # # good - # describe 'User logs in' do - # let(:user) { User.new } - # - # before do - # visit new_session_path - # end - # - # it 'with OAuth' do - # # ... - # end - # end - # - class FeatureMethods < Base - extend AutoCorrector - include InsideExampleGroup - - MSG = 'Use `%s` instead of `%s`.' - - # https://github.com/teamcapybara/capybara/blob/e283c1aeaa72441f5403963577e16333bf111a81/lib/capybara/rspec/features.rb#L31-L36 - MAP = { - background: :before, - scenario: :it, - xscenario: :xit, - given: :let, - given!: :let!, - feature: :describe - }.freeze - - # @!method capybara_speak(node) - def_node_matcher :capybara_speak, <<~PATTERN - {#{MAP.keys.map(&:inspect).join(' ')}} - PATTERN - - # @!method feature_method(node) - def_node_matcher :feature_method, <<~PATTERN - (block - $(send #rspec? $#capybara_speak ...) - ...) - PATTERN - - def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler - return unless inside_example_group?(node) - - feature_method(node) do |send_node, match| - next if enabled?(match) - - add_offense(send_node.loc.selector) do |corrector| - corrector.replace(send_node.loc.selector, MAP[match].to_s) - end - end - end - - def message(range) - name = range.source.to_sym - format(MSG, method: name, replacement: MAP[name]) - end - - private - - def enabled?(method_name) - enabled_methods.include?(method_name) - end - - def enabled_methods - cop_config - .fetch('EnabledMethods', []) - .map(&:to_sym) - end - end - end - end - end -end diff --git a/lib/rubocop/cop/rspec_cops.rb b/lib/rubocop/cop/rspec_cops.rb index 7af420e4e..b4c6cbe9f 100644 --- a/lib/rubocop/cop/rspec_cops.rb +++ b/lib/rubocop/cop/rspec_cops.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require_relative 'rspec/capybara/current_path_expectation' -require_relative 'rspec/capybara/feature_methods' require_relative 'rspec/capybara/match_style' require_relative 'rspec/capybara/negation_matcher' require_relative 'rspec/capybara/specific_actions' diff --git a/spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb b/spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb deleted file mode 100644 index e535a5641..000000000 --- a/spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb +++ /dev/null @@ -1,153 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe RuboCop::Cop::RSpec::Capybara::FeatureMethods do - it 'flags offenses for `background`' do - expect_offense(<<~RUBY) - describe 'some feature' do - background do; end - ^^^^^^^^^^ Use `before` instead of `background`. - end - RUBY - - expect_correction(<<~RUBY) - describe 'some feature' do - before do; end - end - RUBY - end - - it 'flags offenses for `scenario`' do - expect_offense(<<~RUBY) - RSpec.describe 'some feature' do - scenario 'Foo' do; end - ^^^^^^^^ Use `it` instead of `scenario`. - end - RUBY - - expect_correction(<<~RUBY) - RSpec.describe 'some feature' do - it 'Foo' do; end - end - RUBY - end - - it 'flags offenses for `xscenario`' do - expect_offense(<<~RUBY) - describe 'Foo' do - RSpec.xscenario 'Baz' do; end - ^^^^^^^^^ Use `xit` instead of `xscenario`. - end - RUBY - - expect_correction(<<~RUBY) - describe 'Foo' do - RSpec.xit 'Baz' do; end - end - RUBY - end - - it 'flags offenses for `given`' do - expect_offense(<<~RUBY) - RSpec.describe 'Foo' do - given(:foo) { :foo } - ^^^^^ Use `let` instead of `given`. - end - RUBY - - expect_correction(<<~RUBY) - RSpec.describe 'Foo' do - let(:foo) { :foo } - end - RUBY - end - - it 'flags offenses for `given!`' do - expect_offense(<<~RUBY) - describe 'Foo' do - given!(:foo) { :foo } - ^^^^^^ Use `let!` instead of `given!`. - end - RUBY - - expect_correction(<<~RUBY) - describe 'Foo' do - let!(:foo) { :foo } - end - RUBY - end - - it 'flags offenses for `feature`' do - expect_offense(<<~RUBY) - RSpec.feature 'Foo' do; end - ^^^^^^^ Use `describe` instead of `feature`. - RUBY - - expect_correction(<<~RUBY) - RSpec.describe 'Foo' do; end - RUBY - end - - it 'flags offenses inside shared groups' do - expect_offense(<<~RUBY) - RSpec.shared_examples_for 'common scenarios' do - feature 'Foo' do; end - ^^^^^^^ Use `describe` instead of `feature`. - end - RUBY - - expect_correction(<<~RUBY) - RSpec.shared_examples_for 'common scenarios' do - describe 'Foo' do; end - end - RUBY - end - - it 'ignores variables inside examples' do - expect_no_offenses(<<~RUBY) - it 'is valid code' do - given(feature) - assign(background) - run scenario - end - RUBY - end - - it 'ignores feature calls outside spec' do - expect_no_offenses(<<~RUBY) - FactoryBot.define do - factory :company do - feature { "a company" } - background { Faker::Lorem.sentence } - end - end - RUBY - end - - it 'allows includes before the spec' do - expect_offense(<<~RUBY) - require 'rails_helper' - - RSpec.feature 'Foo' do; end - ^^^^^^^ Use `describe` instead of `feature`. - RUBY - end - - context 'with configured `EnabledMethods`' do - let(:cop_config) { { 'EnabledMethods' => %w[feature] } } - - it 'ignores usage of the enabled method' do - expect_no_offenses(<<~RUBY) - RSpec.feature 'feature is enabled' do; end - RUBY - end - - it 'flags other methods' do - expect_offense(<<~RUBY) - RSpec.feature 'feature is enabled' do - given(:foo) { :foo } - ^^^^^ Use `let` instead of `given`. - end - RUBY - end - end -end