Skip to content

Instantly share code, notes, and snippets.

@ajvargo
Created March 1, 2023 15:53
Show Gist options
  • Save ajvargo/58eb9ced7f655c11075971346db10e6b to your computer and use it in GitHub Desktop.
Save ajvargo/58eb9ced7f655c11075971346db10e6b to your computer and use it in GitHub Desktop.
RSpec failure formatter
# It will cycle N dots for progress.
# Failures are printed immediately in detail.
class FailureFormatter
RSpec::Core::Formatters.register self, :example_passed, :example_failed, :dump_summary, :start_dump, :example_pending
@@dot_count = 0
N = 10
attr_reader :output
def initialize(output)
@output = output || StringIO.new
@example_group = nil
end
def example_passed(_notification)
count_example('•', :green)
end
def example_pending(_notification)
count_example('P', :pending)
end
def example_failed(notification)
@@dot_count = 0
clear_line
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap(notification.example.full_description, :failure)
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap(notification.example.location, :blue)
if notification.example.exception.class == RSpec::Expectations::MultipleExpectationsNotMetError
output.puts notification.example.exception.message
else
output.puts notification.example.exception
end
output.puts
output.puts notification.colorized_formatted_backtrace
output.puts
output.puts
end
def start_dump(_notification)
clear_line
output.puts
end
def dump_summary(summary)
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap("=" * 100, :blue)
output.puts summary.colorized_totals_line
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap("-" * 100, :blue)
output.puts
end
private
def count_example(t, c)
clear_line
@@dot_count += 1
output.print RSpec::Core::Formatters::ConsoleCodes.wrap(t * @@dot_count.modulo(N), c)
end
def clear_line
output.print "\e[2K\r"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment