Skip to content

Commit

Permalink
Merge pull request #41 from benlubas/main
Browse files Browse the repository at this point in the history
Correctly run nested describe test blocks
  • Loading branch information
haydenmeade authored Oct 24, 2022
2 parents e9a9f96 + a5b924a commit 2051686
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lua/neotest-jest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ end

local function escapeTestPattern(s)
return (
s
:gsub("%(", "%\\(")
s:gsub("%(", "%\\(")
:gsub("%)", "%\\)")
:gsub("%]", "%\\]")
:gsub("%[", "%\\[")
Expand All @@ -144,6 +143,7 @@ local function escapeTestPattern(s)
:gsub("%$", "%\\$")
:gsub("%^", "%\\^")
:gsub("%/", "%\\/")
:gsub("%'", "%\\'")
)
end

Expand Down Expand Up @@ -195,12 +195,16 @@ function adapter.build_spec(args)
local pos = args.tree:data()
local testNamePattern = "'.*'"

if pos.type == "test" then
testNamePattern = "'" .. escapeTestPattern(pos.name) .. "$'"
end

if pos.type == "namespace" then
testNamePattern = "'^" .. escapeTestPattern(pos.name) .. "'"
if pos.type == "test" or pos.type == "namespace" then
-- pos.id in form "path/to/file::Describe text::test text"
local testName = string.sub(pos.id, string.find(pos.id, "::") + 2)
testName, _ = string.gsub(testName, "::", " ")
testNamePattern = "'^" .. escapeTestPattern(testName)
if pos.type == "test" then
testNamePattern = testNamePattern .. "$'"
else
testNamePattern = testNamePattern .. "'"
end
end

local binary = getJestCommand(pos.path)
Expand Down
11 changes: 11 additions & 0 deletions spec/nestedDescribe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

describe('outer', () => {
describe('inner', () => {
it('should do a thing', () => {
expect('hello').toEqual('hello');
});
it("this has a '", () => {
expect('hello').toEqual('hello');
});
});
});
41 changes: 41 additions & 0 deletions tests/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,45 @@ describe("build_spec", function()
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)

async.it("builds command for nested namespace", function()
local positions = plugin.discover_positions("./spec/nestedDescribe.test.ts"):to_list()

local tree = Tree.from_list(positions, function(pos)
return pos.id
end)

local spec = plugin.build_spec({ tree = tree:children()[1]:children()[1] })

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='^outer inner'")
assert.contains(command, "./spec/nestedDescribe.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)

async.it("builds correct command for test name with ' ", function()
local positions = plugin.discover_positions("./spec/nestedDescribe.test.ts"):to_list()

local tree = Tree.from_list(positions, function(pos)
return pos.id
end)

local spec = plugin.build_spec({ tree = tree:children()[1]:children()[1]:children()[2] })
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='^outer inner this has a \\'$'")
assert.contains(command, "./spec/nestedDescribe.test.ts")
assert.is.truthy(spec.context.file)
assert.is.truthy(spec.context.results_path)
end)
end)

0 comments on commit 2051686

Please sign in to comment.