Disabling code coverage for guard spec runs
Asked Answered
R

3

10

For a variety of reasons, I find that running code coverage every time my files reload from guard is quite a burden. However, there doesn't seem to be a way to conditionally prevent SimpleCov from starting from the spec helper.

Is there a way to disable SimpleCov when run by guard, but not when I run it normally using rake spec?

Richie answered 18/7, 2013 at 2:46 Comment(0)
R
16

I ultimately found this solution:

  1. Add an environment variable in your Guardfile:

    guard :rspec, env: { 'NO_COVERAGE' => 'true' }

  2. Check for it from the spec helper:

    SimpleCov.start :rails unless ENV["NO_COVERAGE"]

Richie answered 18/7, 2013 at 3:59 Comment(1)
The env option is now deprecated, adding the env var to cmd is the now recommended solution, e.g.: guard :rspec, cmd: "NO_COVERAGE=true bin/rspec"Tenotomy
U
4

In your spec helper:

unless ARGV.any? {|e| e =~ /guard-rspec/ }
  SimpleCov.start
end

The idea here is that guard-rspec invokes rspec with a special guard-rspec formatter. Looking for that on the command line given gives you the hint that it was invoked from Guard, so you can just skip SimpleCov if that's there.

Unison answered 18/7, 2013 at 3:27 Comment(0)
F
0

In VsCode, I use Ruby Spec Command: NO_COVERAGE=true bin/rspec at the extension Rails Run Spec

Works too command line: NO_COVERAGE=true bin/rspec spec/*_spec.rb or NO_COVERAGE=true bundle exec rspec spec/*_spec.rb

Fourflusher answered 2/10, 2020 at 13:6 Comment(1)
I use the SimpleCov gemFourflusher

© 2022 - 2024 — McMap. All rights reserved.