Skip to content

Commit ddd92d6

Browse files
authored
Output rspec seed if used (#15)
Rspec supports running the specs in a [random order][1]. This helps in surfacing any accidental dependencies and side effects in the test suite. When debugging test failures caused by such side effects, it's often necessary to reproduce the issue by running the specs in the same order again. For that the seed value needs to be known and logged by the formatter. [1]: https://relishapp.com/rspec/rspec-core/v/3-8/docs/configuration/set-the-order-and-or-seed
1 parent 1895471 commit ddd92d6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/rspec/github/formatter.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
module RSpec
88
module Github
99
class Formatter < RSpec::Core::Formatters::BaseFormatter
10-
RSpec::Core::Formatters.register self, :example_failed, :example_pending
10+
RSpec::Core::Formatters.register self, :example_failed, :example_pending, :seed
1111

1212
def example_failed(failure)
1313
notification = NotificationDecorator.new(failure)
@@ -20,6 +20,12 @@ def example_pending(pending)
2020

2121
output.puts "\n::warning file=#{notification.path},line=#{notification.line}::#{notification.annotation}"
2222
end
23+
24+
def seed(notification)
25+
return unless notification.seed_used?
26+
27+
output.puts notification.fully_formatted
28+
end
2329
end
2430
end
2531
end

spec/rspec/github/formatter_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,26 @@
137137
end
138138
end
139139
end
140+
141+
describe '#seed' do
142+
before { formatter.seed(notification) }
143+
144+
context 'when seed used' do
145+
let(:notification) do
146+
RSpec::Core::Notifications::SeedNotification.new(4242, true)
147+
end
148+
149+
it 'outputs the fully formatted seed notification' do
150+
is_expected.to eq "\nRandomized with seed 4242\n"
151+
end
152+
end
153+
154+
context 'when seed not used' do
155+
let(:notification) do
156+
RSpec::Core::Notifications::SeedNotification.new(nil, false)
157+
end
158+
159+
it { is_expected.to be_empty }
160+
end
161+
end
140162
end

0 commit comments

Comments
 (0)