neotest-jest icon indicating copy to clipboard operation
neotest-jest copied to clipboard

Jest Command Based on Project

Open jrock2004 opened this issue 1 year ago • 5 comments

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

jrock2004 avatar Oct 28 '23 17:10 jrock2004

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

JaapBarnhoorn avatar Nov 01 '23 18:11 JaapBarnhoorn

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

jrock2004 avatar Nov 05 '23 17:11 jrock2004

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,
}

Demianeen avatar Dec 07 '23 13:12 Demianeen

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.

MisanthropicBit avatar Mar 11 '24 21:03 MisanthropicBit

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

jrock2004 avatar Mar 12 '24 11:03 jrock2004