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

Support extra_args #106

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lua/neotest-jest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ end
local getJestCommand = jest_util.getJestCommand
local getJestConfig = jest_util.getJestConfig

---@async
---@param file_path? string
---@return boolean
function adapter.is_test_file(file_path)
Expand Down Expand Up @@ -404,11 +405,16 @@ function adapter.build_spec(args)
local binary = args.jestCommand or getJestCommand(pos.path)
local config = getJestConfig(pos.path) or "jest.config.js"
local command = vim.split(binary, "%s+")

if util.path.exists(config) then
-- only use config if available
table.insert(command, "--config=" .. config)
end

if vim.tbl_islist(args.extra_args) then
vim.list_extend(command, args.extra_args)
end

vim.list_extend(command, {
"--no-coverage",
"--testLocationInResults",
Expand Down
3 changes: 2 additions & 1 deletion tests/init_options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end
local binary_override = function()
return "mybinaryoverride"
end

local config_override = function()
return "./spec/jest.config.ts"
end
Expand All @@ -34,7 +35,7 @@ describe("build_spec with override", function()
assert.contains(command, "--json")
assert.contains(command, "--config=" .. config_override())
assert.contains(command, "--testNamePattern='.*'")
assert.contains(command, "./spec/basic.test.ts")
assert.contains(command, ".\\/spec\\/basic.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
assert.is.same(
Expand Down
40 changes: 32 additions & 8 deletions tests/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ A = function(...)
print(vim.inspect(...))
end

describe("adpter root", function()
describe("adapter root", function()
async.it("jest is installed", function()
assert.Not.Nil(plugin.root("./spec"))
end)
end)

describe("is_test_file", function()
it("matches jest files", function()
async.it("matches jest files", function()
assert.True(plugin.is_test_file("./spec/basic.test.ts"))
end)

it("does not match plain js files", function()
async.it("does not match plain js files", function()
assert.False(plugin.is_test_file("./index.ts"))
end)
end)
Expand Down Expand Up @@ -231,7 +231,31 @@ describe("build_spec", function()
assert.contains(command, "--json")
assert.is_not.contains(command, "--config=jest.config.js")
assert.contains(command, "--testNamePattern='.*'")
assert.contains(command, "./spec/basic.test.ts")
assert.contains(command, ".\\/spec\\/basic.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)

async.it("builds command for file test with extra arguments", function()
local positions = plugin.discover_positions("./spec/basic.test.ts"):to_list()
local tree = Tree.from_list(positions, function(pos)
return pos.id
end)
local spec = plugin.build_spec({
tree = tree,
extra_args = { "--clearCache", "--updateSnapshot" },
})

assert.is.truthy(spec)
local command = spec.command
assert.is.truthy(command)
assert.contains(command, "jest")
assert.contains(command, "--json")
assert.is_not.contains(command, "--config=jest.config.js")
assert.contains(command, "--testNamePattern='.*'")
assert.contains(command, ".\\/spec\\/basic.test.ts")
assert.contains(command, "--clearCache")
assert.contains(command, "--updateSnapshot")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)
Expand All @@ -251,7 +275,7 @@ describe("build_spec", function()
assert.contains(command, "--json")
assert.is_not.contains(command, "--config=jest.config.js")
assert.contains(command, "--testNamePattern='.*'")
assert.contains(command, "./spec/basic.test.ts")
assert.contains(command, ".\\/spec\\/basic.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)
Expand All @@ -272,7 +296,7 @@ describe("build_spec", function()
assert.contains(command, "--json")
assert.is_not.contains(command, "--config=jest.config.js")
assert.contains(command, "--testNamePattern='^describe text'")
assert.contains(command, "./spec/basic.test.ts")
assert.contains(command, ".\\/spec\\/basic.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)
Expand All @@ -293,7 +317,7 @@ describe("build_spec", function()
assert.contains(command, "--json")
assert.is_not.contains(command, "--config=jest.config.js")
assert.contains(command, "--testNamePattern='^outer middle inner'")
assert.contains(command, "./spec/nestedDescribe.test.ts")
assert.contains(command, ".\\/spec\\/nestedDescribe.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)
Expand All @@ -314,7 +338,7 @@ describe("build_spec", function()
assert.contains(command, "--json")
assert.is_not.contains(command, "--config=jest.config.js")
assert.contains(command, "--testNamePattern='^outer middle inner this has a \\'$'")
assert.contains(command, "./spec/nestedDescribe.test.ts")
assert.contains(command, ".\\/spec\\/nestedDescribe.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)
Expand Down
Loading