Skip to content

Commit

Permalink
Merge branch 'main' into parameterized-tests-final
Browse files Browse the repository at this point in the history
  • Loading branch information
KostkaBrukowa authored Aug 24, 2023
2 parents 967cc77 + 66de784 commit ebed0db
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 52 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![build](https://github.com/haydenmeade/neotest-jest/actions/workflows/workflow.yaml/badge.svg)](https://github.com/haydenmeade/neotest-jest/actions/workflows/workflow.yaml)

This plugin provides a jest adapter for the [Neotest](https://github.com/rcarriga/neotest) framework.
**It is currently a work in progress**. It will be transferred to the official neotest organisation (once it's been created).
**It is currently a work in progress**.

## Installation

Expand Down Expand Up @@ -94,6 +94,35 @@ because `jest_test_discovery` runs `jest` command on file to determine
what tests are inside the file. If `discovery` would be enabled then `neotest-jest`
would spawn a lot of procesees.

### Monorepos
If you have a monorepo setup, you might have to do a little more configuration, especially if
you have different jest configurations per package.

```lua
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,
```

Also, if your monorepo set up requires you to run a specific test file with cwd on the package
directory (like when you have a lerna setup for example), you might also have to tweak things a
bit:

```lua
cwd = function()
local file = vim.fn.expand('%:p')
if string.find(file, "/packages/") then
return string.match(file, "(.-/[^/]+/)src")
end
return vim.fn.getcwd()
end
```

## :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
99 changes: 77 additions & 22 deletions lua/neotest-jest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,40 @@ local parameterized_tests = require("neotest-jest.parameterized-tests")
---@type neotest.Adapter
local adapter = { name = "neotest-jest" }

local rootPackageJson = vim.fn.getcwd() .. "/package.json"

---@return boolean
local function rootProjectHasJestDependency()
local path = rootPackageJson

local success, packageJsonContent = pcall(lib.files.read, path)
if not success then
print("cannot read package.json")
return false
end

local parsedPackageJson = vim.json.decode(packageJsonContent)

if parsedPackageJson["dependencies"] then
for key, _ in pairs(parsedPackageJson["dependencies"]) do
if key == "jest" then
return true
end
end
end

if parsedPackageJson["devDependencies"] then
for key, _ in pairs(parsedPackageJson["devDependencies"]) do
if key == "jest" then
return true
end
end
end

return true
end


---@param path string
---@return boolean
local function hasJestDependency(path)
Expand Down Expand Up @@ -49,7 +83,7 @@ local function hasJestDependency(path)
end
end

return false
return rootProjectHasJestDependency()
end

adapter.root = function(path)
Expand Down Expand Up @@ -121,19 +155,31 @@ end
function adapter.discover_positions(path)
local query = [[
; -- Namespaces --
; Matches: `describe('context')`
; Matches: `describe('context', () => {})`
((call_expression
function: (identifier) @func_name (#eq? @func_name "describe")
arguments: (arguments (string (string_fragment) @namespace.name) (arrow_function))
)) @namespace.definition
; Matches: `describe.only('context')`
; Matches: `describe('context', function() {})`
((call_expression
function: (identifier) @func_name (#eq? @func_name "describe")
arguments: (arguments (string (string_fragment) @namespace.name) (function))
)) @namespace.definition
; Matches: `describe.only('context', () => {})`
((call_expression
function: (member_expression
object: (identifier) @func_name (#any-of? @func_name "describe")
)
arguments: (arguments (string (string_fragment) @namespace.name) (arrow_function))
)) @namespace.definition
; Matches: `describe.each(['data'])('context')`
; Matches: `describe.only('context', function() {})`
((call_expression
function: (member_expression
object: (identifier) @func_name (#any-of? @func_name "describe")
)
arguments: (arguments (string (string_fragment) @namespace.name) (function))
)) @namespace.definition
; Matches: `describe.each(['data'])('context', () => {})`
((call_expression
function: (call_expression
function: (member_expression
Expand All @@ -142,19 +188,28 @@ function adapter.discover_positions(path)
)
arguments: (arguments (string (string_fragment) @namespace.name) (arrow_function))
)) @namespace.definition
; Matches: `describe.each(['data'])('context', function() {})`
((call_expression
function: (call_expression
function: (member_expression
object: (identifier) @func_name (#any-of? @func_name "describe")
)
)
arguments: (arguments (string (string_fragment) @namespace.name) (function))
)) @namespace.definition
; -- Tests --
; Matches: `test('test') / it('test')`
((call_expression
function: (identifier) @func_name (#any-of? @func_name "it" "test")
arguments: (arguments (string (string_fragment) @test.name) (arrow_function))
arguments: (arguments (string (string_fragment) @test.name) [(arrow_function) (function)])
)) @test.definition
; Matches: `test.only('test') / it.only('test')`
((call_expression
function: (member_expression
object: (identifier) @func_name (#any-of? @func_name "test" "it")
)
arguments: (arguments (string (string_fragment) @test.name) (arrow_function))
arguments: (arguments (string (string_fragment) @test.name) [(arrow_function) (function)])
)) @test.definition
; Matches: `test.each(['data'])('test') / it.each(['data'])('test')`
((call_expression
Expand All @@ -164,7 +219,7 @@ function adapter.discover_positions(path)
property: (property_identifier) @each_property (#eq? @each_property "each")
)
)
arguments: (arguments (string (string_fragment) @test.name) (arrow_function))
arguments: (arguments (string (string_fragment) @test.name) [(arrow_function) (function)])
)) @test.definition
]]

Expand All @@ -189,17 +244,17 @@ end
local function escapeTestPattern(s)
return (
s:gsub("%(", "%\\(")
:gsub("%)", "%\\)")
:gsub("%]", "%\\]")
:gsub("%[", "%\\[")
:gsub("%*", "%\\*")
:gsub("%+", "%\\+")
:gsub("%-", "%\\-")
:gsub("%?", "%\\?")
:gsub("%$", "%\\$")
:gsub("%^", "%\\^")
:gsub("%/", "%\\/")
:gsub("%'", "%\\'")
:gsub("%)", "%\\)")
:gsub("%]", "%\\]")
:gsub("%[", "%\\[")
:gsub("%*", "%\\*")
:gsub("%+", "%\\+")
:gsub("%-", "%\\-")
:gsub("%?", "%\\?")
:gsub("%$", "%\\$")
:gsub("%^", "%\\^")
:gsub("%/", "%\\/")
:gsub("%'", "%\\'")
)
end

Expand Down Expand Up @@ -240,10 +295,10 @@ end

local function cleanAnsi(s)
return s:gsub("\x1b%[%d+;%d+;%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+m", "")
:gsub("\x1b%[%d+m", "")
:gsub("\x1b%[%d+;%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+;%d+m", "")
:gsub("\x1b%[%d+;%d+m", "")
:gsub("\x1b%[%d+m", "")
end

local function findErrorPosition(file, errStr)
Expand Down
18 changes: 18 additions & 0 deletions spec/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ describe("describe text", () => {
console.log("do test");
});
});

describe("describe text 2", function() {
it.each([1, 2, 3])("Array1", function() {
console.log("do test");
});

it.each([1, 2, 3])("Array2", async function() {
console.log("do test");
});

test.each([1, 2, 3])("Array3", function() {
console.log("do test");
});

test.each([1, 2, 3])("Array4", async function() {
console.log("do test");
});
});
18 changes: 18 additions & 0 deletions spec/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ describe("describe text", () => {
console.log("do test");
});
});

describe("describe text 2", function() {
it("1", function() {
console.log("do test");
});

it("2", async function() {
console.log("do test");
});

test("3", function() {
console.log("do test");
});

test("4", async function() {
console.log("do test");
});
})
16 changes: 9 additions & 7 deletions spec/nestedDescribe.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

describe('outer', () => {
describe('inner', () => {
it('should do a thing', () => {
expect('hello').toEqual('hello');
describe('middle', function() {
describe('inner', () => {
it('should do a thing', () => {
expect('hello').toEqual('hello');
});
it("this has a '", () => {
expect('hello').toEqual('hello');
});
});
it("this has a '", () => {
expect('hello').toEqual('hello');
});
});
})
});
Loading

0 comments on commit ebed0db

Please sign in to comment.