nvim-neotest / neotest

An extensible framework for interacting with tests within NeoVim.
MIT License
2.13k stars 105 forks source link

`vim-test`/`neotest-vim-test` : `test#project_root` is not changing `Working Directory` (monorepo) #359

Open ls-devs opened 5 months ago

ls-devs commented 5 months ago

Hi, Actually working in a monorepo, I have tests in my_project/frontend/tests. If I cd into my_project/frontend tests are found and neotest is working perfectly. But If I only cd into the root directory of my project, test are not found.

I saw this on vim-test documentation :

Working directory

Test.vim relies on you being cd-ed into the project root. However, sometimes you may want to execute tests from a different directory than Vim's current working directory. You might have a bigger project or monorepo with many subprojects, or you might be using autochdir. In any case, you can tell test.vim to use a different working directory for running tests:

let test#project_root = "/path/to/your/project"

Unfortunately, if I set test#project_root to my_project/frontent or my_project/frontend/tests and only cd into the root directory of my project ( my_project/). Tests aren't found.

How can I make vim-test / neotest-vim-test found my tests folder without always cd in the direct parent folder of my tests ? (here my_project/frontend)

EDIT : Found a workaround using

vim.cmd("cd ./frontend")

But shouldn't let test#project_root = "/path/to/your/project" do the job ?

juansalvatore commented 2 months ago

@ls-devs hey! I was having the same issue and just found a work around that worked for me, hopefully it works for you too.

        ['neotest-vitest'] = {
          vitestCommand = 'npx vitest run',
          vitestConfigFile = function(file)
            if string.find(file, '/packages/') then
              return string.match(file, '(.-/[^/]+/)src') .. 'vitest.config.ts'
            end

            return vim.fn.getcwd() .. '/vitest.config.ts'
          end,
        },