summaryrefslogtreecommitdiff
path: root/qa/qa/specs/helpers/rspec.rb
blob: 3e97dbd118aa12dd0d5271bc96daff10f6ef88a0 (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
# frozen_string_literal: true

require 'rspec/core'

module QA
  module Specs
    module Helpers
      module RSpec
        # We need a reporter for internal tests that's different from the reporter for
        # external tests otherwise the results will be mixed up. We don't care about
        # most reporting, but we do want to know if a test fails
        class RaiseOnFailuresReporter < ::RSpec::Core::NullReporter
          def self.example_failed(example)
            raise example.exception
          end
        end

        # We use an example group wrapper to prevent the state of internal tests
        # expanding into the global state
        # See: https://github.com/rspec/rspec-core/issues/2603
        def describe_successfully(*args, &describe_body)
          example_group = ::RSpec.describe(*args, &describe_body)
          ran_successfully = example_group.run reporter
          expect(ran_successfully).to eq true
          example_group
        end

        def send_stop_notification
          reporter.notify(
            :stop,
            ::RSpec::Core::Notifications::ExamplesNotification.new(reporter)
          )
        end

        def reporter
          ::RSpec.configuration.reporter
        end
      end
    end
  end
end