nvim-neotest / neotest-jest

MIT License
116 stars 79 forks source link

Jest Command Based on Project #86

Open jrock2004 opened 10 months ago

jrock2004 commented 10 months ago

Not an issue per-say but wondering how people configure their jest command when you have projects that use npm, some that use yarn, etc... Do you change your config? Or is there a dynamic way?

return {
  "nvim-neotest/neotest",
  dependencies = {
    "haydenmeade/neotest-jest",
  },
  opts = function(_, opts)
    table.insert(
      opts.adapters,
      require("neotest-jest")({
        -- jestCommand = 'npm test --',
        jestCommand = "pnpm test --",
        -- jestCommand = "yarn test",
        -- jestConfigFile = 'custom.jest.config.ts',
        env = { CI = true },
        cwd = function()
          return vim.fn.getcwd()
        end,
      })
    )
  end,
}

Update 1:

I think when I made this ticket, I was bad in not adding a thought on a solution. What if we do something like not pass jestCommand and have another property that is something like:

...,
detectCommand = {
  enable = true,
  args = "test --"
},
...

With this, what the plugin can do is check in the root dir of project, is there a package-lock, or yarn.lock, or pnpm-lock? Then it could build the jestCommand from that

JaapBarnhoorn commented 10 months ago

Although not exactly what you are looking for, but you could use ni to automatically use the correct package manager.

For example running ‘nr test —‘ will run your test regardless of your package manager.

https://www.builder.io/blog/goodbye-package-manager-chaos

jrock2004 commented 10 months ago

Although not exactly what you are looking for, but you could use ni to automatically use the correct package manager.

For example running ‘nr test —‘ will run your test regardless of your package manager.

https://www.builder.io/blog/goodbye-package-manager-chaos

Thanks, but was hoping to not have another toolchain. I might go with this but hoping for another solution

Demianeen commented 9 months ago

Does option with nr work for you? In my case tests are passing, but neotest still show that test failed: CleanShot 2023-12-07 at 13 10 10@2x

I didn't add any fancy settings and everything works with jestCommand = "npm test --":

 {
    jestCommand = "nr test --",
    jestConfigFile = function()
        local file = vim.fn.expand("%:p")
        if string.find(file, "/packages/") then
            return string.match(file, "(.-/[^/]+/)src")
                .. "jest.config.ts"
        end

        return vim.fn.getcwd() .. "/jest.config.ts"
    end,
    env = { CI = true },
    cwd = function(path)
        return vim.fn.getcwd()
    end,
}
MisanthropicBit commented 5 months ago

Not sure if this is still relevant but the jestCommand option can also take a function accepting a single argument (the path of the current test). So you could do:

jestCommand = function(path)
    local cwd = vim.fn.getcwd()

    if vim.endswith(cwd, 'my-special-project') then
        return "yarn test"
    end

    return "npm test"
end

You might also be able to adapt some of the code for hasJestDependency or some of the util functions to locate the package.json file, or similar, and grab the test command directly.

jrock2004 commented 5 months ago

@MisanthropicBit interesting. Maybe I could check for lock file.