ruby-lsp icon indicating copy to clipboard operation
ruby-lsp copied to clipboard

Fix discovery of test methods in specs

Open thomasmarshall opened this issue 1 month ago • 2 comments

Motivation

Closes #3722.

Methods starting test_ are picked up my Minitest but were not being discovered by the LSP.

describe "MySpec" do
  def test_foo
    assert true
  end
end

Implementation

This commit adds a #on_def_node_enter handler to register test items for methods starting test_ in describe blocks. It should not match non-test methods (e.g. helper methods), nor should it match methods named test_ in non-spec blocks.

Automated Tests

I have added a new testfor this fix.

Manual Tests

  1. Add a spec with a test_ method
  2. See that it is discovered by the LSP
Before After
Screenshot 2025-12-04 at 13 03 13 Screenshot 2025-12-04 at 13 04 10

thomasmarshall avatar Dec 04 '25 13:12 thomasmarshall

I think this may cause duplicate test items in this case below. The reason is because both the TestStyle and SpecStyle listeners run together and so if we always handle test_ methods, it will end up duplicating.

class MySpec < Minitest::Spec
  def test_something; end # is this one duplicated?
end

Can you please check if we're doing the right thing in this case?

vinistock avatar Dec 08 '25 13:12 vinistock

Hmm in this case it's not duplicated (because we check current_group.is_a?(DescribeGroup)) but I found a case where it is duplicated, where we have a describe inside a spec class:

class MySpec < Minitest::Spec
  describe 'something' do
    def test_something; end # this one is duplicated
  end
end

I'll work on a fix.

thomasmarshall avatar Dec 08 '25 14:12 thomasmarshall