Skip to content

Print rspec progress to standard out when using 'run in terminal' #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
# frozen_string_literal: true

require "rspec/core/formatters"
require "rspec/core/formatters/progress_formatter"
require "ruby_lsp/test_reporters/lsp_reporter"

module RubyLsp
module RSpec
class RSpecFormatter
class RSpecFormatter < ::RSpec::Core::Formatters::ProgressFormatter
::RSpec::Core::Formatters.register(
self,
:example_passed,
:example_pending,
:example_failed,
:example_started,
:start_dump,
:stop,
)

def initialize(output)
@output = output
super(output)
end

def example_started(notification)
Expand All @@ -29,20 +31,26 @@ def example_started(notification)
end

def example_passed(notification)
super(notification)

example = notification.example
uri = uri_for(example)
id = generate_id(example)
RubyLsp::LspReporter.instance.record_pass(id: id, uri: uri)
end

def example_failed(notification)
super(notification)

example = notification.example
uri = uri_for(example)
id = generate_id(example)
RubyLsp::LspReporter.instance.record_fail(id: id, message: notification.exception.message, uri: uri)
end

def example_pending(notification)
super(notification)

example = notification.example
uri = uri_for(example)
id = generate_id(example)
Expand Down
61 changes: 61 additions & 0 deletions spec/rspec_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require "socket"
require "open3"
require "json"
require "stringio"
require "ruby_lsp/ruby_lsp_rspec/rspec_formatter"

RSpec.describe "RubyLsp::RSpec::RSpecFormatter" do
it "sends correct LSP events during test execution" do
Expand Down Expand Up @@ -135,4 +137,63 @@

expect(events).to eq(expected)
end

describe "RubyLsp::RSpec::RSpecFormatter notifications" do
let(:output) { StringIO.new }
let(:formatter) { RubyLsp::RSpec::RSpecFormatter.new(output) }
let(:notification) { double("Notification") }
let(:example) { double("Example") }

before do
allow(notification).to receive(:example).and_return(example)
allow(example).to receive(:file_path).and_return("spec/fixtures/rspec_example_spec.rb")
allow(example).to receive(:location).and_return("./spec/fixtures/rspec_example_spec.rb:13")
allow(example).to receive(:example_group).and_return(double("ExampleGroup", parent_groups: []))
end

it "is a subclass of ProgressFormatter" do
expect(RubyLsp::RSpec::RSpecFormatter.superclass).to eq(RSpec::Core::Formatters::ProgressFormatter)
end

it "registers necessary notifications with RSpec" do
registered_notifications = RSpec::Core::Formatters::Loader.formatters[RubyLsp::RSpec::RSpecFormatter]

expect(registered_notifications).to match_array([
:example_passed,
:example_pending,
:example_failed,
:example_started,
:start_dump,
:stop,
])
end

it "invokes ProgressFormatter's example_passed" do
expect_any_instance_of(RSpec::Core::Formatters::ProgressFormatter).to receive(:example_passed)

formatter.example_passed(notification)
end

it "invokes ProgressFormatter's example_failed" do
exception = double("Exception", message: "error message")
allow(notification).to receive(:exception).and_return(exception)

expect_any_instance_of(RSpec::Core::Formatters::ProgressFormatter).to receive(:example_failed)

formatter.example_failed(notification)
end

it "invokes ProgressFormatter's example_pending" do
expect_any_instance_of(RSpec::Core::Formatters::ProgressFormatter).to receive(:example_pending)

formatter.example_pending(notification)
end

it "invokes ProgressFormatter's start_dump" do
dump_notification = double("DumpNotification")
expect_any_instance_of(RSpec::Core::Formatters::ProgressFormatter).to receive(:start_dump)

formatter.start_dump(dump_notification)
end
end
end
Loading