nvim-neotest / neotest-jest

MIT License
116 stars 79 forks source link

Neovim freezes when using jest-util.getJestCommand #119

Open chris-av opened 4 months ago

chris-av commented 4 months ago

Hello there!

My neovim/terminal gets absolutely bricked when I setup neotest-jest. I've identified the source of the issue as being the jest-util.getJestCommand function. This only happens when I am in a non-javascript project. I am only able to exit by forcefully killing the neovim process or my terminal entirely.

Here is my understanding of how this plugin is supposed to work: I set up my adapters for each language. The plugin will identify what kind of project I am in and use the corresponding adapter. Therefore, if I use the getJestCommand in my config, it should not run when I open a python project, since there shouldn't be a jest command to be found. Please correct me if I am wrong about how this works!

So with that in mind, here is my setup:

require('neotest').setup({
  adapters = {
    require('neotest-jest')({
      jest_test_discovery = false,
      jestCommand = require('neotest-jest.jest-util').getJestCommand(vim.fn.expand '%:p:h') .. ' --watch', -- bricks neovim
      -- jestCommand = 'jest --watch' -- this is what I use currently to avoid neovim freezing
    }),
    require("neotest-python"),
  },
})

I can abate this behavior by putting in a guard statement at the top of the foundBinary function within getJestCommand:

function M.getJestCommand(path)
  local gitAncestor = util.find_git_ancestor(path)

  local function findBinary(p)
    -- NOTE: my addition, this guard statement prevents infinite recursion??
    if p == nil then
      return
    end

    local rootPath = util.find_node_modules_ancestor(p)
    local jestBinary = util.path.join(rootPath, "node_modules", ".bin", "jest")

    if util.path.exists(jestBinary) then
      return jestBinary
    end

    -- If no binary found and the current directory isn't the parent
    -- git ancestor, let's traverse up the tree again
    if rootPath ~= gitAncestor then
      return findBinary(util.path.dirname(rootPath))
    end
  end

  local foundBinary = findBinary(path)

  if foundBinary ~= nil then
    print('foundBinary : ' .. foundBinary)
  end

  if foundBinary then
    return foundBinary
  end

  return "jest"
end

Here is my system:

OS: Arch Linux x86_64 Kernel: 6.8.7-arch1-1 CPU: Intel i5-10310U (8) @ 4.400GHz GPU: Intel CometLake-U GT2 [UHD Graphics] nvim version : 0.9.5

Using nvm to manage node versions. I can verify that I have installed jest globally with npm list -g. I also had jest installed as a local dev dependency in this project in question as well. I used Lazy to update neotest, neotest-jest, and plenary to the latest version.

I typically start neovim (nvim .) from the root of the project, which had this general structure:

.
├── jest.config.ts
├── package.json
├── package-lock.json
├── pnpm-lock.yaml
├── README.md
├── src
│   ├── module
│   │   ├── index.test.ts
│   │   └── index.ts
└── tsconfig.json

Also attaching a video. First I demonstrate that I am able to successfully open a Javascript project like normal. Tests can run and everything is dandy. Then I got to a Rust project and open neovim from the root directory. You can see the subsequent freezing/brick behavior.

https://github.com/nvim-neotest/neotest-jest/assets/41182258/1c60e0f7-c1b9-4d7c-b2cf-e15910a0bdfb