Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(coverage-recipe): add front end coverage for jest #401

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:changes'] = '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