Code Coverage highlighting
Note
This is an excerpt from https://jmatuszewski.com/Automatically-run-Rails-tests-in-VS-Code-and-highlight-test-coverage/.
Todo
Make it work with minitest!
- Install the Coverage Gutters extension and click the Watch icon added to the footer
- Add
Simplecov
andSimplecov-lcov
to Gemfile:
group :test do
gem 'simplecov'
gem 'simplecov-lcov'
end
- Add the
lcov
formatter to SimpleCov initialization:
# spec/test_helper.rb
SimpleCov::Formatter::LcovFormatter.config do |c|
c.report_with_single_file = true
c.output_directory = 'coverage'
c.lcov_file_name = 'lcov.info'
end
SimpleCov.start do
enable_coverage :branch
formatter SimpleCov::Formatter::MultiFormatter.new(
[
SimpleCov::Formatter::LcovFormatter,
SimpleCov::Formatter::HTMLFormatter
]
)
end
Voila! Now when hitting CTRL + S
on any file from /app
folder I do get coverage higlights in gutter:
Extension Configuration
Every project is different so it might be that you have to change some configuration options as it’s set up to support RSpec by default:
railsAutomaticTestRunner.framework
: Specifies which framework is used to execute test commands:rspec
orminitest
.railsAutomaticTestRunner.testsDirectory
: Specifies the folder where your test files are located, the default directory isspec
.railsAutomaticTestRunner.bundleExec
: Appendbundle exec
before test command?railsAutomaticTestRunner.envVariables
: Pass any environment variables as a string there, eg:CRUCIAL_API_KEY=test
railsAutomaticTestRunner.args
: Pass any arguments like--fail-fast
as a string thererailsAutomaticTestRunner.automaticOutputDisplay
: Automatically switch to the output tab, when enabled might interrupt terminal work.