https://github.com/ylecuyer/sprockets-js-coverage
Add coverage to your JavaScript files
https://github.com/ylecuyer/sprockets-js-coverage
Last synced: 20 days ago
JSON representation
Add coverage to your JavaScript files
- Host: GitHub
- URL: https://github.com/ylecuyer/sprockets-js-coverage
- Owner: ylecuyer
- License: mit
- Created: 2023-04-03T12:43:15.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-12-02T13:27:05.000Z (5 months ago)
- Last Synced: 2025-03-23T23:47:42.185Z (about 1 month ago)
- Language: Ruby
- Homepage:
- Size: 38.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Installation
Add this line to your application's Gemfile:
```ruby
gem 'sprockets-js-coverage'
```Add the processor to sprockets
```ruby
# config/initializers/assets.rbif ENV["COVERAGE"]
Sprockets::Js::Coverage::Processor.configure do |config|
config.should_process = ->(path) {
return false if path.match?(/vendor\/assets\//)
return false if path.match?(/gems\//)
return true
}
endRails.application.config.assets.configure do |env|
env.register_postprocessor('application/javascript', Sprockets::Js::Coverage::Processor)
end
end
```Get the coverage reports after running your tests
```ruby
# test/application_system_test_case.rbrequire "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
def teardown
__coverage__ = page.evaluate_script <<-JS
JSON.stringify((typeof __coverage__ !== 'undefined') ? __coverage__ : null)
JSif __coverage__ != "null"
File.write("#{JS_COVERAGE_DIR}/#{Time.now.to_i.to_s}.json", __coverage__)
endsuper
end
end
```Convert to lcov and generate html report
```bash
nyc report --reporter=lcov --temp-dir tmp/js-coverage --report-dir tmp/js-coverage
genhtml -q -o ./coverage ./tmp/js-coverage/lcov.info ./coverage/lcov/simplecov.lcov
```# How it works
This gem uses istanbul instrumenter to add coverage to your javascript files. It will add a global variable `__coverage__` to your javascript files. You can then use this variable to get the coverage report.
# Configuration
TBD
# Contributing
Clone the repo and run `bundle install` to install the dependencies.
Prepare the js script by running `yarn install` and `yarn run build` in the `js/instrumenter` directory.
# Release
Run `./scripts/release.sh` to release a new version after updating the version in `lib/sprockets/js/coverage/version.rb`.