jscov 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f29b1e1e112c912b6a7213210d3f156c9ca57e2ec7e01a2c5c9c1770051d7156
4
- data.tar.gz: 5db3557ea568399e5de364d93fa6f7ba6beaad43f91b5e3dc2bf10d7beba48ef
3
+ metadata.gz: 1309a526d1abd161ae4991ae627b9057a67d6d6bc9b42bb6d9d1fdc30cd43596
4
+ data.tar.gz: 5dc48bd325b4baad9229f58cb8ede061b2936f803fe1f288f5118b2e889bacb6
5
5
  SHA512:
6
- metadata.gz: ca06dc98ada7976f0edaab3935cb89c1ea8808f0f40b16ba444350f7f97c6ddad2a5a2804d0c359924f00c411a46403bbf0a80ccf91608f2dacfde4a5d959f45
7
- data.tar.gz: 413eb1782c2cc65351c3b28fadb030d5fc79d4a3747dce8cb31596acc3f66ae68791a398d2774eaf0b099f02b2865ec8a0701bf14a6963eb78064ae31d67e8d6
6
+ metadata.gz: 64c0472f4d002c4403e653a94d7907037de2a96a1ca757e2c69b999acb714830d9f4bc130533b83167427b9db55ce6b9a79d6ee2969564c0fee4429a40f1b07e
7
+ data.tar.gz: c23a2c965d76b558daa086d2a649ae970e34236fe6837f12803db4d33733e818782bfb1923a8c3a5ef729cc730e1db2bf2fab6ddd127343c99638632077f1343
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Gem Version](https://badge.fury.io/rb/jscov.svg)](http://badge.fury.io/rb/jscov)
2
+ [![Build Status](https://travis-ci.org/kzkn/jscov.svg?branch=master)](https://travis-ci.org/kzkn/jscov)
3
+
1
4
  # Jscov
2
5
 
3
6
  Collect JavaScript code coverages. It works with [istanbul](https://istanbul.js.org/).
@@ -41,23 +44,14 @@ module.exports = function (api) {
41
44
  }
42
45
  ```
43
46
 
44
- Mount `Jscov::Engine`. In route.rb:
47
+ Add Jscov middleware to rails middleware stack. Add the following in `config/environments/test.rb`:
45
48
 
46
49
  ```ruby
47
- if Jscov.enabled?
48
- mount Jscov::Engine => "/jscov"
49
- end
50
- ```
51
-
52
- Insert `jscov_script_tag` in your layout file:
53
-
54
- ```html
55
- <html>
56
- <head>
57
- <%= jscov_script_tag %>
58
- </head>
50
+ [MyRailsApp]::Application.configure do |config|
51
+ ...
52
+ config.middleware.use Jscov::RackMiddleware
59
53
  ...
60
- </html>
54
+ end
61
55
  ```
62
56
 
63
57
  Load rspec helper. In spec/rails_helper.rb:
@@ -79,19 +73,46 @@ The collected coverages can be output as a report using [nyc](https://github.com
79
73
  $ npx nyc report --temp-dir=tmp/jscov
80
74
  ```
81
75
 
76
+ ### Vue.js
77
+
78
+ To collect coverage for `.vue` files, you will need to change the configurations.
79
+
80
+ In babel.config.js:
81
+
82
+ ```js
83
+ module.exports = function (api) {
84
+ // ....
85
+ return {
86
+ plugins: [
87
+ // ...
88
+ // Add below
89
+ isTestEnv && [
90
+ 'babel-plugin-istanbul',
91
+ {
92
+ extension: ['.js', '.vue']
93
+ }
94
+ ]
95
+ ].filter(Boolean)
96
+ }
97
+ }
98
+ ```
99
+
100
+ And, you need to add an argument to `nyc`:
101
+
102
+ ```bash
103
+ $ npx nyc report --temp-dir=tmp/jscov --extension=.vue
104
+ ```
105
+
82
106
  ## Configuration
83
107
 
84
- In `config/initializers/jscov.rb`, you can configure the following values.
108
+ You can configure the following values.
85
109
 
86
110
  ```ruby
87
111
  Jscov.configure do |config|
88
- # config.enabled = Rails.env.test?
89
112
  # config.coverage_report_dir_path = Rails.root.join("tmp/jscov")
113
+ # config.coverages_path = "/jscov/coverages"
90
114
  end
91
115
  ```
92
116
 
93
- ## Contributing
94
- Contribution directions go here.
95
-
96
117
  ## License
97
118
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,10 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
- require "rake/testtask"
2
+ require "rspec/core/rake_task"
3
3
 
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.pattern = "test/**/*_test.rb"
7
- t.verbose = false
8
- end
4
+ RSpec::Core::RakeTask.new(:spec)
9
5
 
10
- task default: :test
6
+ task default: :spec
@@ -1,7 +1,6 @@
1
1
  require "securerandom"
2
- require "jscov/engine"
3
- require "jscov/railtie"
4
2
  require "jscov/configuration"
3
+ require "jscov/rack_middleware"
5
4
 
6
5
  module Jscov
7
6
  class << self
@@ -13,12 +12,6 @@ module Jscov
13
12
  yield configuration
14
13
  end
15
14
 
16
- delegate :enabled?, to: :configuration
17
-
18
- def new_coverage_file_path
19
- configuration.coverage_report_dir_path.join("jscov_#{Time.current.to_i}_#{SecureRandom.uuid}.json")
20
- end
21
-
22
15
  def clean!
23
16
  dir_path = configuration.coverage_report_dir_path
24
17
  FileUtils.rm_f(Dir.glob(dir_path.join("jscov_*.json")))
@@ -0,0 +1,63 @@
1
+ module Jscov
2
+ class Bless
3
+ def initialize(response)
4
+ @response = response
5
+ end
6
+
7
+ def result
8
+ [
9
+ @response[0],
10
+ @response[1],
11
+ blessed_body
12
+ ]
13
+ end
14
+
15
+ private
16
+
17
+ def blessed_body
18
+ plain_body = @response[2]
19
+ head, body = find_head_tag(plain_body.dup)
20
+ body.unshift(bless(head))
21
+ end
22
+
23
+ def find_head_tag(plain_body)
24
+ head = ''
25
+ remaining = []
26
+ found = false
27
+ plain_body.each do |body|
28
+ if found
29
+ remaining << body
30
+ else
31
+ head << body
32
+ found = head.include?("<head>")
33
+ end
34
+ end
35
+
36
+ [head, remaining]
37
+ end
38
+
39
+ def bless(html)
40
+ html.gsub!("<head>", "<head><script>#{self.class.js_code}</script>")
41
+ html
42
+ end
43
+
44
+ class << self
45
+ def js_code
46
+ <<~JS
47
+ (function () {
48
+ window.addEventListener("unload", uploadCoverage)
49
+
50
+ function uploadCoverage () {
51
+ const cov = window.__coverage__
52
+ if (!cov) { return }
53
+
54
+ const data = new FormData()
55
+ data.append("coverage", JSON.stringify(cov))
56
+ navigator.sendBeacon("#{Jscov.configuration.coverages_path}", data)
57
+ }
58
+ })()
59
+ JS
60
+ end
61
+ end
62
+ end
63
+ end
@@ -1,16 +1,24 @@
1
1
  module Jscov
2
2
  class Configuration
3
- attr_accessor :enabled, :coverage_report_dir_path
3
+ attr_accessor :coverage_report_dir_path, :coverages_path
4
4
 
5
5
  def initialize
6
6
  reset!
7
7
  end
8
8
 
9
- alias enabled? enabled
10
-
11
9
  def reset!
12
- self.enabled = Rails.env.test?
13
- self.coverage_report_dir_path = Rails.root.join("tmp/jscov")
10
+ self.coverage_report_dir_path = self.class.default_coverage_report_dir_path
11
+ self.coverages_path = "/jscov/coverages"
12
+ end
13
+
14
+ class << self
15
+ def default_coverage_report_dir_path
16
+ if defined?(Rails)
17
+ Rails.root.join("tmp/jscov")
18
+ else
19
+ "tmp/jscov"
20
+ end
21
+ end
14
22
  end
15
23
  end
16
24
  end
@@ -5,9 +5,15 @@ module Jscov
5
5
  end
6
6
 
7
7
  def save
8
- File.open(Jscov.new_coverage_file_path, "w") do |f|
8
+ File.open(self.class.new_coverage_file_path, "w") do |f|
9
9
  f.puts @raw_data.to_json
10
10
  end
11
11
  end
12
+
13
+ class << self
14
+ def new_coverage_file_path
15
+ Jscov.configuration.coverage_report_dir_path.join("jscov_#{Time.current.to_i}_#{SecureRandom.uuid}.json")
16
+ end
17
+ end
12
18
  end
13
19
  end
@@ -0,0 +1,36 @@
1
+ require "rack/request"
2
+ require "jscov/coverage"
3
+ require "jscov/bless"
4
+
5
+ module Jscov
6
+ class RackMiddleware
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ request = ::Rack::Request.new(env)
13
+ if jscov_request?(request)
14
+ handle(request)
15
+ else
16
+ response = @app.call(env)
17
+ Bless.new(response).result
18
+ end
19
+ end
20
+
21
+ def jscov_request?(request)
22
+ request.request_method == "POST" && request.path == coverages_path
23
+ end
24
+
25
+ def coverages_path
26
+ Jscov.configuration.coverages_path
27
+ end
28
+
29
+ def handle(request)
30
+ raw_data = JSON.parse(request.params["coverage"])
31
+ coverage = Jscov::Coverage.new(raw_data)
32
+ coverage.save
33
+ [204, {}, []]
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module Jscov
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,45 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jscov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazuki Nishikawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-27 00:00:00.000000000 Z
11
+ date: 2020-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: railties
14
+ name: rack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.2'
19
+ version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.2'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: actionview
28
+ name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '5.2'
34
- type: :runtime
33
+ version: '0'
34
+ type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '5.2'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: sqlite3
42
+ name: capybara
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -52,7 +52,55 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Jscov is a Rails Engine for collecting JavaScript code coverages.
55
+ - !ruby/object:Gem::Dependency
56
+ name: webdrivers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: selenium-webdriver
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '5.2'
90
+ - - "<"
91
+ - !ruby/object:Gem::Version
92
+ version: '7.0'
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '5.2'
100
+ - - "<"
101
+ - !ruby/object:Gem::Version
102
+ version: '7.0'
103
+ description: A rack middleware for collecting JavaScript code coverages.
56
104
  email:
57
105
  - kzkn@users.noreply.github.com
58
106
  executables: []
@@ -62,18 +110,13 @@ files:
62
110
  - MIT-LICENSE
63
111
  - README.md
64
112
  - Rakefile
65
- - app/controllers/jscov/application_controller.rb
66
- - app/controllers/jscov/coverages_controller.rb
67
- - config/routes.rb
68
113
  - lib/jscov.rb
114
+ - lib/jscov/bless.rb
69
115
  - lib/jscov/configuration.rb
70
116
  - lib/jscov/coverage.rb
71
- - lib/jscov/engine.rb
72
- - lib/jscov/helper.rb
73
- - lib/jscov/railtie.rb
117
+ - lib/jscov/rack_middleware.rb
74
118
  - lib/jscov/rspec.rb
75
119
  - lib/jscov/version.rb
76
- - lib/tasks/jscov_tasks.rake
77
120
  homepage: https://github.com/kzkn/jscov
78
121
  licenses:
79
122
  - MIT
@@ -1,5 +0,0 @@
1
- module Jscov
2
- class ApplicationController < ActionController::Base
3
- protect_from_forgery with: :exception
4
- end
5
- end
@@ -1,14 +0,0 @@
1
- require "jscov/coverage"
2
-
3
- module Jscov
4
- class CoveragesController < ApplicationController
5
- skip_before_action :verify_authenticity_token
6
-
7
- def create
8
- raw_data = JSON.parse(params[:coverage])
9
- coverage = Jscov::Coverage.new(raw_data)
10
- coverage.save
11
- head :no_content
12
- end
13
- end
14
- end
@@ -1,3 +0,0 @@
1
- Jscov::Engine.routes.draw do
2
- resources :coverages, only: %i[create]
3
- end
@@ -1,5 +0,0 @@
1
- module Jscov
2
- class Engine < ::Rails::Engine
3
- isolate_namespace Jscov
4
- end
5
- end
@@ -1,26 +0,0 @@
1
- module Jscov
2
- module Helper
3
- def jscov_script_tag
4
- return unless Jscov.enabled?
5
-
6
- javascript_tag jscov_javascript_code
7
- end
8
-
9
- def jscov_javascript_code
10
- <<~JS
11
- (function () {
12
- window.addEventListener('unload', uploadCoverage)
13
-
14
- function uploadCoverage () {
15
- const cov = window.__coverage__
16
- if (!cov) { return }
17
-
18
- const data = new FormData()
19
- data.append('coverage', JSON.stringify(cov))
20
- navigator.sendBeacon('#{jscov.coverages_path}', data)
21
- }
22
- })()
23
- JS
24
- end
25
- end
26
- end
@@ -1,11 +0,0 @@
1
- require "jscov/helper"
2
-
3
- module Jscov
4
- class Railtie < ::Rails::Railtie
5
- initializer "jscov" do
6
- ActiveSupport.on_load(:action_view) do
7
- include Helper
8
- end
9
- end
10
- end
11
- end
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :jscov do
3
- # # Task goes here
4
- # end