-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathspec_helper.rb
More file actions
169 lines (142 loc) Β· 5.7 KB
/
spec_helper.rb
File metadata and controls
169 lines (142 loc) Β· 5.7 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require 'simplecov'
require 'simplecov-lcov'
require 'shoulda/matchers'
require 'webmock/rspec'
# Fix incompatibility of simplecov-lcov with older versions of simplecov that are not expresses in its gemspec.
# https://github.com/fortissimo1997/simplecov-lcov/pull/25
if !SimpleCov.respond_to?(:branch_coverage)
module SimpleCov
def self.branch_coverage?
false
end
end
end
SimpleCov::Formatter::LcovFormatter.config do |c|
c.report_with_single_file = true
c.single_report_path = 'coverage/lcov.info'
end
SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new(
[
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::LcovFormatter,
]
)
SimpleCov.start do
add_filter 'spec/'
# Support parallel test execution
# In CI: Use CI_NODE_INDEX (0, 1, 2, 3) set by GitHub Actions matrix
# Locally: Use TEST_ENV_NUMBER ('', '2', '3', '4') set by parallel_tests
if ENV['CI_NODE_INDEX']
command_name "RSpec-#{ENV['CI_NODE_INDEX']}"
use_merging true
merge_timeout 3600
elsif ENV.key?('TEST_ENV_NUMBER')
# TEST_ENV_NUMBER is '' for first process, '2', '3', etc. for others
suffix = ENV['TEST_ENV_NUMBER'].empty? ? '1' : ENV['TEST_ENV_NUMBER']
command_name "RSpec-#{suffix}"
use_merging true
merge_timeout 3600
end
end
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
# Block all external HTTP requests in tests; allows localhost for Capybara
WebMock.disable_net_connect!(allow_localhost: true)
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.check_all_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include ActiveJob::TestHelper
config.include ApplicationHelper
config.include LoginHelpers
config.include ActiveSupport::Testing::TimeHelpers
config.include SelectFromChosen, type: :feature
config.infer_spec_type_from_file_location!
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.order = 'random'
config.expect_with :rspec do |c|
c.syntax = :expect
end
# See https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example
config.before(:suite) do
if config.use_transactional_fixtures?
raise(<<-MSG)
Delete line `config.use_transactional_fixtures = true` from spec_helper.rb
(or set it to false) to prevent uncommitted transactions being used in
JavaScript-dependent specs.
During testing, the app-under-test that the browser driver connects to
uses a different database connection to the database connection used by
the spec. The app's database connection would not be able to access
uncommitted transaction data setup over the spec's database connection.
MSG
end
DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.strategy = :deletion
end
config.before(:each) do
# Stub all Flodesk API endpoints globally so tests don't make external requests
# when fabricating members (which trigger Subscription.after_create callback)
WebMock.stub_request(:any, %r{api\.flodesk\.com}).
to_return(status: 200, body: '{"status":"active","segments":[]}', headers: { 'Content-Type' => 'application/json' })
DatabaseCleaner.strategy = :transaction
clear_enqueued_jobs
clear_performed_jobs
end
# Driver is using an external browser with an app
# under test that does *not* share a database connection with the
# specs, so use truncation strategy. This config is order dependent
# and must be BELOW the main `config.before(:each)` configuration
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
# This block must be here, do not combine with the other `config.before(:each)` block.
# This makes it so Capybara can see the database.
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.after do |example|
# Take a screenshot if the example failed and JavaScript is enabled
if example.exception && defined?(page) && Capybara.current_driver == :playwright
# Get the filename and line number of the failing spec
location = example.metadata[:location]
filename, line_number = location.split(':')
# Build a custom filename for the screenshot
screenshot_filename = "#{filename}:#{line_number}.png"
# Save the screenshot using the custom filename
page.save_screenshot(screenshot_filename)
end
end
config.example_status_persistence_file_path = 'tmp/spec_failures'
if Bullet.enable?
config.around(:each) do |example|
Bullet.start_request
example.run
Bullet.perform_out_of_channel_notifications if Bullet.notification?
Bullet.end_request
end
end
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
CarrierWave.configure do |config|
config.enable_processing = false
config.storage = :file
# NOTE: We're taking this out for now because it results in:
# ActionController::RoutingError:
# No route matches [GET] "/uploads/sponsor/avatar/1/thumb_the-guardian.png"
# ...
# Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true
#
# Ideally there would be a clear seperation between production uploads and testing uploads. Testing uploads would also be cleaned up
# see https://til.codes/testing-carrierwave-file-uploads-with-rspec-and-factorygirl/.
# Funny enough this issue has been present for a while. For some reason Capybara wasnraising server errors!
# config.root = Rails.root.join('tmp')
end