Skip to content

Commit

Permalink
feat(coverage-recipe): add front end coverage for jest
Browse files Browse the repository at this point in the history
  • Loading branch information
FloValdes committed Apr 5, 2022
1 parent e2998b3 commit 435cc9d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
26 changes: 26 additions & 0 deletions lib/potassium/recipes/coverage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ def create
load_gems
configure_rails_helper
append_to_file('.gitignore', "/coverage/*\n")
recipe = self
after(:setup_jest) do
recipe.configure_jest_coverage
end
end

def installed?
Expand All @@ -13,6 +17,14 @@ def install
create
end

def configure_jest_coverage
json_file = File.read(Pathname.new("package.json"))
js_package = JSON.parse(json_file)
js_package = add_coverage_config(js_package)
json_string = JSON.pretty_generate(js_package)
create_file 'package.json', json_string, force: true
end

private

def load_gems
Expand All @@ -32,4 +44,18 @@ def configure_rails_helper
end
end
end

def add_coverage_config(js_package)
js_package['scripts']['test'] = 'jest --changedSince=master'
js_package['jest'] = js_package['jest'].merge(coverage_defaults)
js_package
end

def coverage_defaults
{
collectCoverage: true,
collectCoverageFrom: ['**/*.{js,ts,vue}', '!**/node_modules/**'],
coverageReporters: ['text']
}
end
end
7 changes: 4 additions & 3 deletions lib/potassium/recipes/front_end.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ def setup_vue
copy_file '../assets/app/javascript/components/app.vue', 'app/javascript/components/app.vue'
copy_file '../assets/app/javascript/types/vue.d.ts', 'app/javascript/types/vue.d.ts'
setup_vue_with_compiler_build
setup_jest
if get(:api) == :graphql
setup_apollo
recipe = self
run_action(:setup_jest) do
recipe.setup_jest
end
setup_apollo if get(:api) == :graphql
end

private
Expand Down
12 changes: 12 additions & 0 deletions spec/features/coverage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@
content = IO.read("#{project_path}/spec/simplecov_config.rb")
expect(content).to include("SimpleCov.start 'rails'")
end

context "with vue" do
before(:all) do
remove_project_directory
create_dummy_project("front_end" => "vue")
end

it "adds jest coverage configuration" do
node_modules_file = IO.read("#{project_path}/package.json")
expect(node_modules_file).to include('"collectCoverage": true')
end
end
end

0 comments on commit 435cc9d

Please sign in to comment.