marilari88 / neotest-vitest

Vitest adapter for Neovim Neotest plugin
113 stars 34 forks source link

No tests found if test is outside of `__tests__` directory #22

Open h0adp0re opened 1 year ago

h0adp0re commented 1 year ago

We have a __tests__ folder in the root of our project for legacy reasons and the actual tests for our components are in the components' sub-folders within src. How to proceed?

Edit: you should probably check whether the filename contains spec or test before checking whether the file is in __tests__.

marilari88 commented 1 year ago

Hi @h0adp0re! I tried to repro your case and everything works fine (as you can see in the picture below). Can you tell me if you are experiencing any problems discovering the tests or running them? Do you see the tests in the summary panel? The order in which the adapter checks for the existence of the tests folder should not affect the result of the is_test_file function.

image

h0adp0re commented 1 year ago

Hey, that's strange. I found that after saving the file once, everything works as expected, but only for that file. Everything works as expected globally when I delete the __tests__ directory from root. 🤷‍♂️

But, let's pretend I don't save the file first nor delete anything. The summary panel actually does show 2 directories — __tests__ and src.

I tested a bit and here are some summary panel observations:

  1. Running a test in a test file via require("neotest").run.run() doesn't seem to do anything, although the editor slows down a bit for a few seconds; something must be happening in the background.
  2. Opening the summary panel in a test file:
    • None of the directories/suites/tests are expanded in the summary panel.
  3. When I expand the directories manually:
    • None of the tests in a file are identified/listed in the summary.
    • Only the test file is listed in the directory, nothing inside of it.
  4. Running the file (with summary.mappings.run) results in:
    • The file passing successfully.
    • The icon for the successfully run test file becoming "skipped".

After the initial run via the summary panel comes the interesting part:

  1. Saving the test file buffer:
    • Causes "passed" neotest symbols to render in the gutter for every test, which indicates that something just started working.
  2. Running tests via require("neotest").run.run() works as expected.
  3. Re-opening the summary panel or moving back to it:
    • Every single test in the file is listed under it.
  4. Re-running the file in the summary panel:
    • The "passed" icon is rendered for the file and every suite and test under it.

Sounds like some initialization or perhaps an autocommand problem on my side?


Edit: I tried to make the text more structured, hope it's not too confusing.

h0adp0re commented 9 months ago

Alright well, thanks @AlexandrosAlexiou! #43 helped fix this issue for me. I simply told neotest-vitest to only consider what it finds in the src folder as tests in my special project.

Although I would like to confirm that I'm using this correctly. Is the true default sensible?

adapters = {
  require("neotest-vitest")({
    is_test_file = function(file_path)
      if string.match(file_path, "my-project") then
        return string.match(file_path, "/src/")
      end

      return true
    end,
  }),
},

And more, reading the source suggests that this check runs in addition to the usual checks? Would moving this condition as the first one in the routine save some needless parsing in unrelated directories? https://github.com/marilari88/neotest-vitest/blob/c0ea475596483eb02fa8e92c6be65c0536d55630/lua/neotest-vitest/init.lua#L430-L435