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

Configurable test patterns #111

Open
wants to merge 9 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
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ require('neotest').setup({
adapters = {
require('neotest-jest')({
...,
jest_test_discovery = false,
jest_test_discovery = true,
}),
}
})
```
Its also recommended to disable `neotest` `discovery` option like this:
It's also recommended to disable `neotest` `discovery` option like this:
```lua
require("neotest").setup({
...,
Expand Down Expand Up @@ -123,6 +123,37 @@ cwd = function(file)
end
```

### Custom test extensions

If the default test extensions don't match your test patterns, you can provide
your own:

```lua
require('neotest').setup({
...,
adapters = {
require('neotest-jest')({
...,
extension_test_file_match = require('neotest-jest.util').create_test_file_extensions_matcher({ "perf", "spec" }, { "ts", "tsx" })
}),
}
})
```

The call to `create_test_file_extensions_matcher` will return a function that will match
`".perf.ts"`, `".perf.tsx"`, `".spec.ts"`, and `".spec.tsx"` test patterns.

Note that the default test extensions won't be used for matching anymore. If you
want to extend the defaults, you can retrieve them.

```lua
local intermediate_extensions, extensions =
require('neotest-jest.util').default_test_extensions()
```

Here, `intermediate_extensions` are the extensions like `perf` and `spec` and
`extensions` is `ts` and `tsx` in the example above.

## :gift: Contributing

Please raise a PR if you are interested in adding new functionality or fixing any bugs. When submitting a bug, please include an example spec that can be tested.
Expand Down
22 changes: 10 additions & 12 deletions lua/neotest-jest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local parameterized_tests = require("neotest-jest.parameterized-tests")
---@field env? table<string, string>|fun(): table<string, string>
---@field cwd? string|fun(): string
---@field strategy_config? table<string, unknown>|fun(): table<string, unknown>
---@field extension_test_file_match fun(file_path: string): boolean

---@type neotest.Adapter
local adapter = { name = "neotest-jest" }
Expand Down Expand Up @@ -99,29 +100,22 @@ end

local getJestCommand = jest_util.getJestCommand
local getJestConfig = jest_util.getJestConfig
local intermediate_extensions, extensions = util.default_test_extensions()
local extension_test_file_match =
util.create_test_file_extensions_matcher(intermediate_extensions, extensions)

---@param file_path? string
---@return boolean
function adapter.is_test_file(file_path)
if file_path == nil then
return false
end
local is_test_file = false

if string.match(file_path, "__tests__") then
is_test_file = true
return hasJestDependency(file_path)
end

for _, x in ipairs({ "spec", "e2e%-spec", "test", "unit", "regression", "integration" }) do
for _, ext in ipairs({ "js", "jsx", "coffee", "ts", "tsx" }) do
if string.match(file_path, "%." .. x .. "%." .. ext .. "$") then
is_test_file = true
goto matched_pattern
end
end
end
::matched_pattern::
return is_test_file and hasJestDependency(file_path)
return extension_test_file_match(file_path) and hasJestDependency(file_path)
end

function adapter.filter_dir(name)
Expand Down Expand Up @@ -524,6 +518,10 @@ setmetatable(adapter, {
end
end

if is_callable(opts.extension_test_file_match) then
extension_test_file_match = opts.extension_test_file_match
end

if opts.jest_test_discovery then
adapter.jest_test_discovery = true
end
Expand Down
35 changes: 35 additions & 0 deletions lua/neotest-jest/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ function M.find_node_modules_ancestor(startpath)
end
end)
end

function M.find_package_json_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
if M.path.is_file(M.path.join(path, "package.json")) then
return path
end
end)
end

function M.find_git_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
-- .git is a file when the project is a git worktree
Expand Down Expand Up @@ -239,4 +241,37 @@ function M.stream(file_path)
return queue.get, exit_future.set
end

---@return string[]
---@return string[]
function M.default_test_extensions()
return { "spec", "e2e%-spec", "test", "unit", "regression", "integration" }, {
"js",
"jsx",
"coffee",
"ts",
"tsx",
}
end

---@param intermediate_extensions string[]
---@param end_extensions string[]
---@return fun(file_path: string): boolean
function M.create_test_file_extensions_matcher(intermediate_extensions, end_extensions)
return function(file_path)
if file_path == nil then
return false
end

for _, iext in ipairs(intermediate_extensions) do
for _, eext in ipairs(end_extensions) do
if string.match(file_path, "%." .. iext .. "%." .. eext .. "$") then
return true
end
end
end

return false
end
end

return M
2 changes: 1 addition & 1 deletion tests/init_options_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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
87 changes: 79 additions & 8 deletions tests/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,97 @@ local async = require("nio").tests
local plugin = require("neotest-jest")({
jestCommand = "jest",
})
local util = require("neotest-jest.util")
local Tree = require("neotest.types").Tree
require("neotest-jest-assertions")
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)

it("gets default test extensions", function()
local intermediate_extensions, extensions = util.default_test_extensions()

assert.same(
intermediate_extensions,
{ "spec", "e2e%-spec", "test", "unit", "regression", "integration" }
)
assert.same(extensions, { "js", "jsx", "coffee", "ts", "tsx" })
end)

async.it("matches test files with default test patterns", function()
stub(require("neotest.lib").files, "match_root_pattern", function()
return function(path)
return path
end
end)

stub(require("neotest.lib").files, "read", function()
return [[
{
"name": "spec",
"version": "1.0.0",
"description": "neotest-jest spec",
"main": "index.js",
"license": "MIT",
"dependencies": {
"jest": "^28.1.2",
"ts-node": "^10.8.2",
"typescript": "^4.7.4"
},
"devDependencies": {
"@types/jest": "^28.1.4",
"@types/node": "^18.0.3"
}
}]]
end)

local intermediate_extensions, extensions = util.default_test_extensions()

for _, extension1 in ipairs(intermediate_extensions) do
for _, extension2 in ipairs(extensions) do
local stripped_path, _ = ("./spec/basic." .. extension1 .. "." .. extension2):gsub(
"%%%-",
"%-"
)

assert.True(plugin.is_test_file(stripped_path))
end
end

require("neotest.lib").files.match_root_pattern:revert()
require("neotest.lib").files.read:revert()
end)

async.it("matches test files with configurable test patterns", function()
local intermediate_extensions = { "spec", "lollipop" }
local extensions = { "js", "ts" }
local is_test_file =
util.create_test_file_extensions_matcher(intermediate_extensions, extensions)

for _, extension1 in ipairs(intermediate_extensions) do
for _, extension2 in ipairs(extensions) do
assert.True(is_test_file("./spec/basic." .. extension1 .. "." .. extension2))
end
end

-- Does not match anymore with custom extensions
assert.False(is_test_file("./spec/basic.test.ts"))
end)
end)

describe("discover_positions", function()
Expand Down Expand Up @@ -231,7 +302,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 @@ -251,7 +322,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 +343,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 +364,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 +385,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