Skip to content

Instantly share code, notes, and snippets.

@ajvargo
Created December 30, 2021 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajvargo/f435a10fa755ab7bb90c2f4eec055ccd to your computer and use it in GitHub Desktop.
Save ajvargo/f435a10fa755ab7bb90c2f4eec055ccd to your computer and use it in GitHub Desktop.
Custom RSpec formatter
# I don't like a lot of noise when my specs run.
# This will print the characters from 'progress', but clears the line every 10 so you don't get an epic line of dots.
#
# Any failures are printed as you go, so I can start looking while the the tests finish.
#
# You can simplify this with your .rspec-local file.
# rspec --require path/to/failure_formatter.rb -- format FailureFormatter spec
class FailureFormatter
RSpec::Core::Formatters.register self, :example_passed, :example_failed, :dump_summary, :start_dump, :example_pending
@@dot_count = 0
N = 10 # how many dots before we clear the line?
attr_reader :output
def initialize(output)
@output = output || StringIO.new
end
def example_passed(_notification)
count_example('•', :success)
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
output.puts notification.example.exception.message
else
output.puts notification.example.exception
end
output.puts
end
def start_dump(_notification)
clear_line
output.puts
end
def dump_summary(summary)
output.puts summary.colorized_totals_line
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