Skip to content

Commit 8be915d

Browse files
Fix discovery of test methods in specs
This commit ensures methods starting with "test_" are discovered as test methods in specs.
1 parent 92213c2 commit 8be915d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

lib/ruby_lsp/listeners/spec_style.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def initialize(response_builder, global_state, dispatcher, uri)
2626
register_events(
2727
dispatcher,
2828
:on_class_node_enter,
29+
:on_def_node_enter,
2930
:on_call_node_enter,
3031
:on_call_node_leave,
3132
)
@@ -56,6 +57,31 @@ def on_module_node_leave(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerM
5657
super
5758
end
5859

60+
#: (Prism::DefNode) -> void
61+
def on_def_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
62+
name = node.name.to_s
63+
return unless name.start_with?("test_")
64+
65+
current_group = @spec_group_id_stack.last
66+
return unless current_group.is_a?(DescribeGroup)
67+
68+
parent = latest_group
69+
return unless parent.is_a?(Requests::Support::TestItem)
70+
71+
id = "#{parent.id}##{name}"
72+
73+
test_item = Requests::Support::TestItem.new(
74+
id,
75+
name,
76+
@uri,
77+
range_from_node(node),
78+
framework: :minitest,
79+
)
80+
81+
parent.add(test_item)
82+
@response_builder.add_code_lens(test_item)
83+
end
84+
5985
#: (Prism::CallNode) -> void
6086
def on_call_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
6187
case node.name

test/requests/discover_tests_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,36 @@ module OtherNamespace
822822
end
823823
end
824824

825+
def test_discovers_test_methods
826+
source = <<~RUBY
827+
describe "MySpec" do
828+
def test_foo; end
829+
def helper_method; end
830+
831+
describe "nested" do
832+
def test_nested; end
833+
end
834+
end
835+
836+
class NotASpec
837+
def test_ignored; end
838+
end
839+
RUBY
840+
841+
with_minitest_spec_configured(source) do |items|
842+
assert_equal(["MySpec"], items.map { |i| i[:id] })
843+
assert_equal(
844+
["MySpec#test_foo", "MySpec::nested"],
845+
items.dig(0, :children).map { |i| i[:id] },
846+
)
847+
assert_equal(
848+
["MySpec::nested#test_nested"],
849+
items.dig(0, :children, 1, :children).map { |i| i[:id] },
850+
)
851+
assert_all_items_tagged_with(items, :minitest)
852+
end
853+
end
854+
825855
private
826856

827857
def create_test_discovery_addon

0 commit comments

Comments
 (0)