From 435cc9d029648d75d206fc8779e48d7bba3131b5 Mon Sep 17 00:00:00 2001 From: FloValdes Date: Fri, 1 Apr 2022 18:26:13 -0300 Subject: [PATCH] feat(coverage-recipe): add front end coverage for jest --- lib/potassium/recipes/coverage.rb | 26 ++++++++++++++++++++++++++ lib/potassium/recipes/front_end.rb | 7 ++++--- spec/features/coverage_spec.rb | 12 ++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/potassium/recipes/coverage.rb b/lib/potassium/recipes/coverage.rb index a87e5b0e..90546c48 100644 --- a/lib/potassium/recipes/coverage.rb +++ b/lib/potassium/recipes/coverage.rb @@ -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? @@ -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 @@ -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 diff --git a/lib/potassium/recipes/front_end.rb b/lib/potassium/recipes/front_end.rb index 2238dd1f..e2e4df74 100644 --- a/lib/potassium/recipes/front_end.rb +++ b/lib/potassium/recipes/front_end.rb @@ -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 diff --git a/spec/features/coverage_spec.rb b/spec/features/coverage_spec.rb index daf336fc..4fdc20cb 100644 --- a/spec/features/coverage_spec.rb +++ b/spec/features/coverage_spec.rb @@ -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