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

Jest Command Based on Project #86

Open
jrock2004 opened this issue Oct 28, 2023 · 5 comments
Open

Jest Command Based on Project #86

jrock2004 opened this issue Oct 28, 2023 · 5 comments

Comments

@jrock2004
Copy link

jrock2004 commented Oct 28, 2023

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

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

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

Demianeen commented Dec 7, 2023

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

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

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

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

4 participants