Skip to content

Commit

Permalink
feat: add position_type to allow more rspec_cmd customization (#62)
Browse files Browse the repository at this point in the history
* Adds position type to get_rspec_cmd call

* Adds examples of position_type to README.md

* Adds more detailed documentation in README for position_type
  • Loading branch information
nolantait authored Mar 19, 2024
1 parent 0d73fe6 commit 1b6a43a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,57 @@ require("neotest-rspec")({
})
```

One issue you can run into is when you use generated tests:

```ruby
RSpec.describe SomeClass do
describe "some feature" do
FIXTURES.each do |method, path|
it "does something with the #{method} and #{path}" do
# ...
end
end
end
end
```

The test will show as passing if any one of the tests pass instead of if all of
them pass. You can change the test command to use `--fail-fast` to fix this.
However now your other tests will all fail if even a single test fails.

To solve this you can customize your command depending on the type of test you
are running by using the optional positional argument `position_type`


```lua
require("neotest-rspec")({
-- Optionally your function can take a position_type which is one of:
-- - "file"
-- - "test"
-- - "dir"
rspec_cmd = function(position_type)
if position_type == "test" then
return vim.tbl_flatten({
"bundle",
"exec",
"rspec",
"--fail-fast"
})
else
return vim.tbl_flatten({
"bundle",
"exec",
"rspec",
})
end
end
})
```

Now when you run your tests from a single test it will fail fast and show the
correct error on your generated tests. When you run the whole file it won't fail
fast and a single error won't cause all your other tests to fail.

### Setting the root directory

For Neotest adapters to work, they need to define a project root whereby the process of discovering tests can take place. By default, the adapter looks for a `Gemfile`, `.rspec` or `.gitignore` file. These can be changed with:
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-rspec/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function NeotestAdapter.build_spec(args)
if position.type == "dir" and vim.bo.filetype == "neotest-summary" then run_by_filename() end

local command = vim.tbl_flatten({
config.get_rspec_cmd(),
config.get_rspec_cmd(position.type),
script_args,
})

Expand Down

0 comments on commit 1b6a43a

Please sign in to comment.