Skip to content
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

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

Open
h0adp0re opened this issue Mar 13, 2023 · 3 comments
Open

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

h0adp0re opened this issue Mar 13, 2023 · 3 comments

Comments

@h0adp0re
Copy link

h0adp0re commented Mar 13, 2023

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__.

@h0adp0re h0adp0re changed the title No tests found if __tests__ directory is in the root No tests found if test is outside of __tests__ directory Mar 13, 2023
@marilari88
Copy link
Owner

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
Copy link
Author

h0adp0re commented Mar 13, 2023

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
Copy link
Author

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?

if is_callable(opts.is_test_file) then
local is_test_file = adapter.is_test_file
adapter.is_test_file = function(file_path)
return is_test_file(file_path) and opts.is_test_file(file_path)
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants