summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/rspec/shared_groups_metadata_spec.rb
blob: 3dd568e7dcd36ef78fb50a69ce4fcb47643a394e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

require 'rubocop_spec_helper'
require 'rspec-parameterized'

require_relative '../../../../rubocop/cop/rspec/shared_groups_metadata'

RSpec.describe RuboCop::Cop::RSpec::SharedGroupsMetadata, feature_category: :tooling do
  context 'with hash metadata' do
    it 'flags metadata in shared example' do
      expect_offense(<<~RUBY)
          RSpec.shared_examples 'foo', feature_category: :shared do
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388
          end

          shared_examples 'foo', feature_category: :shared do
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388
          end
      RUBY
    end

    it 'flags metadata in shared context' do
      expect_offense(<<~RUBY)
          RSpec.shared_context 'foo', feature_category: :shared do
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388
          end

          shared_context 'foo', feature_category: :shared do
                                ^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388
          end
      RUBY
    end
  end

  context 'with symbol metadata' do
    it 'flags metadata in shared example' do
      expect_offense(<<~RUBY)
          RSpec.shared_examples 'foo', :aggregate_failures do
                                       ^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388
          end

          shared_examples 'foo', :aggregate_failures do
                                 ^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388
          end
      RUBY
    end

    it 'flags metadata in shared context' do
      expect_offense(<<~RUBY)
          RSpec.shared_context 'foo', :aggregate_failures do
                                      ^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388
          end

          shared_context 'foo', :aggregate_failures do
                                ^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388
          end
      RUBY
    end
  end

  it 'does not flag if feature category is missing' do
    expect_no_offenses(<<~RUBY)
        RSpec.shared_examples 'foo' do
        end

        shared_examples 'foo' do
        end
    RUBY
  end
end