From a350f877c4246fee981690388239d1e19e17202a Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Fri, 11 Jun 2021 09:09:58 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- rubocop/cop/rspec/have_enqueued_sidekiq_job.rb | 60 ++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 rubocop/cop/rspec/have_enqueued_sidekiq_job.rb (limited to 'rubocop') diff --git a/rubocop/cop/rspec/have_enqueued_sidekiq_job.rb b/rubocop/cop/rspec/have_enqueued_sidekiq_job.rb new file mode 100644 index 00000000000..4b0beb10723 --- /dev/null +++ b/rubocop/cop/rspec/have_enqueued_sidekiq_job.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module RSpec + # This cop checks for `expect(worker).to receive(:perform_async)` usage in specs + # + # @example + # # bad + # it "enqueues a worker" do + # expect(MyWorker).to receive(:perform_async) + # end + # + # # good + # it "enqueues a worker" do + # expect(MyWorker).to have_enqueued_sidekiq_job + # end + # + # # bad + # it "enqueues a worker" do + # expect(MyWorker).to receive(:perform_async).with(1, 2) + # end + # + # # good + # it "enqueues a worker" do + # expect(MyWorker).to have_enqueued_sidekiq_job(1, 2) + # end + # + class HaveEnqueuedSidekiqJob < RuboCop::Cop::Cop + MESSAGE = 'Do not use `receive(:perform_async)` to check a job has been enqueued, use `have_enqueued_sidekiq_job` instead.' + + def_node_search :expect_to_receive_perform_async?, <<~PATTERN + (send + (send nil? :expect ...) + {:to :not_to :to_not} + { + (send nil? :receive (sym :perform_async)) + + (send + (send nil? :receive (sym :perform_async)) ... + ) + + (send + (send + (send nil? :receive (sym :perform_async)) ... + ) ... + ) + } + ) + PATTERN + + def on_send(node) + return unless expect_to_receive_perform_async?(node) + + add_offense(node, location: :expression, message: MESSAGE) + end + end + end + end +end -- cgit v1.2.3