rspec-core
rspec-core copied to clipboard
Allow config for rerun location as line number for shared or nested examples
Related to https://github.com/rspec/rspec-core/issues/2119#issuecomment-2105371152
What is the change?
The out of the box behaviour has not changed, but a new configuration option has been added ✨
config.use_line_number_for_failed_spec_location_rerun
The end result is an output which is easier for humans and non-rspec computers (like vim
or vscode
) to jump to the location of the failure 🧑💻
Say you use a shared_example
(or setup your specs inside a loop):
# spec/temp_spec.rb
RSpec.describe do
shared_examples_for 'a failing spec' do
it 'fails' do
expect(1).to eq(2)
end
end
context 'the first context' do
it_behaves_like 'a failing spec'
end
context 'the second context' do
it_behaves_like 'a failing spec'
end
end
The default output will look something like:
rspec './spec/temp_spec.rb[1:1:1:1]' # the first context behaves like a failing spec fails
rspec './spec/temp_spec.rb[1:2:1:1]' # the second context behaves like a failing spec fails
This PR adds the ability to instead print the failure line number:
rspec './spec/temp_spec.rb:10' # the first context behaves like a failing spec fails
rspec './spec/temp_spec.rb:14' # the second context behaves like a failing spec fails
What if the shared example is defined in another file?
As you can see in the above, the failure line number is the line where it_behaves_like
is called from, and this is the case if the shared_example
is defined elsewhere (see new specs).
Note that there is no 'ideal' situation which will match the perfect amount of information in the CLI output to the ease of re-running and subsequent debugging of specs. The current solution allows for easy re-running of tests, but is not human readable, and the solution implemented in this PR is still easily re-runnable, and a bit more readable, but at the cost of requiring a little bit more inspection in your text editor 😄