jscov 0.2.0 → 0.3.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: 1309a526d1abd161ae4991ae627b9057a67d6d6bc9b42bb6d9d1fdc30cd43596
4
- data.tar.gz: 5dc48bd325b4baad9229f58cb8ede061b2936f803fe1f288f5118b2e889bacb6
3
+ metadata.gz: 99ef4a868465e2904e7c3630c95b7e65d279156394278ad540e7de000547f1d5
4
+ data.tar.gz: c2dd6c216c26c20e5061d76a68ae0c2136ff9832723185a5d089e27c0a842b60
5
5
  SHA512:
6
- metadata.gz: 64c0472f4d002c4403e653a94d7907037de2a96a1ca757e2c69b999acb714830d9f4bc130533b83167427b9db55ce6b9a79d6ee2969564c0fee4429a40f1b07e
7
- data.tar.gz: c23a2c965d76b558daa086d2a649ae970e34236fe6837f12803db4d33733e818782bfb1923a8c3a5ef729cc730e1db2bf2fab6ddd127343c99638632077f1343
6
+ metadata.gz: 8425216d48ef1a6f01995530d8756f49bfa55a382bbe327d2efb563cde9d3d3f202cbe4d32700bb2c4835d247de9d9628206194fdfb86c1f8d0619946be3fda4
7
+ data.tar.gz: '029e8f9733aab260bed599f2133accc86b00db24062fbf421cd44b35730ef0163b4f821e2032c0546d302e33422f6208591c6b0d554b3fa692a9177c218962d1'
data/README.md CHANGED
@@ -60,6 +60,17 @@ Load rspec helper. In spec/rails_helper.rb:
60
60
  require 'jscov/rspec'
61
61
  ```
62
62
 
63
+ Configure selenium to capture the output of `console.log`:
64
+
65
+ ```ruby
66
+ RSpec.configure do |config|
67
+ config.before(type: :system) do |example|
68
+ caps = Selenium::WebDriver::Remote::Capabilities.chrome('chromeOptions' => { w3c: false }, 'goog:loggingPrefs' => { browser: 'ALL' })
69
+ driven_by :selenium, using: :headless_chrome, options: { desired_capabilities: caps }
70
+ end
71
+ end
72
+ ```
73
+
63
74
  Run `NODE_ENV=test RAILS_ENV=test bin/rails assets:precompile` for generating js codes that applied `istanbul`:
64
75
 
65
76
  ```bash
@@ -110,7 +121,6 @@ You can configure the following values.
110
121
  ```ruby
111
122
  Jscov.configure do |config|
112
123
  # config.coverage_report_dir_path = Rails.root.join("tmp/jscov")
113
- # config.coverages_path = "/jscov/coverages"
114
124
  end
115
125
  ```
116
126
 
@@ -7,55 +7,62 @@ module Jscov
7
7
  def result
8
8
  [
9
9
  @response[0],
10
- @response[1],
11
- blessed_body
10
+ headers,
11
+ blessed_body,
12
12
  ]
13
13
  end
14
14
 
15
15
  private
16
16
 
17
+ def headers
18
+ @response[1]
19
+ end
20
+
17
21
  def blessed_body
18
22
  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
23
+ return plain_body unless html?
24
+
25
+ blessed = []
26
+ plain_body.each do |fragment|
27
+ blessed << bless(fragment)
34
28
  end
35
29
 
36
- [head, remaining]
30
+ plain_body.close if plain_body.respond_to?(:close)
31
+
32
+ blessed
37
33
  end
38
34
 
39
- def bless(html)
40
- html.gsub!("<head>", "<head><script>#{self.class.js_code}</script>")
41
- html
35
+ def html?
36
+ content_type = headers["Content-Type"]
37
+ content_type =~ /text\/html/
38
+ end
39
+
40
+ def bless(fragment)
41
+ index = fragment.index(/<head>/i)
42
+ if index
43
+ fragment.insert(index + "<head>".size, script_tag)
44
+ else
45
+ fragment
46
+ end
47
+ end
48
+
49
+ def script_tag
50
+ tag = "<script>#{self.class.js_code}</script>"
51
+ tag = tag.html_safe if tag.respond_to?(:html_safe)
52
+ tag
42
53
  end
43
54
 
44
55
  class << self
45
56
  def js_code
46
57
  <<~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
- })()
58
+ window.addEventListener("unload", __jscov_dumpCoverage)
59
+
60
+ function __jscov_dumpCoverage() {
61
+ const cov = window.__coverage__
62
+ if (!cov) { return }
63
+
64
+ console.log('__jscov', JSON.stringify(cov))
65
+ }
59
66
  JS
60
67
  end
61
68
  end
@@ -1,6 +1,6 @@
1
1
  module Jscov
2
2
  class Configuration
3
- attr_accessor :coverage_report_dir_path, :coverages_path
3
+ attr_accessor :coverage_report_dir_path
4
4
 
5
5
  def initialize
6
6
  reset!
@@ -8,7 +8,6 @@ module Jscov
8
8
 
9
9
  def reset!
10
10
  self.coverage_report_dir_path = self.class.default_coverage_report_dir_path
11
- self.coverages_path = "/jscov/coverages"
12
11
  end
13
12
 
14
13
  class << self
@@ -1,3 +1,5 @@
1
+ require "time"
2
+
1
3
  module Jscov
2
4
  class Coverage
3
5
  def initialize(raw_data)
@@ -12,7 +14,8 @@ module Jscov
12
14
 
13
15
  class << self
14
16
  def new_coverage_file_path
15
- Jscov.configuration.coverage_report_dir_path.join("jscov_#{Time.current.to_i}_#{SecureRandom.uuid}.json")
17
+ name = "jscov_#{Time.now.to_i}_#{SecureRandom.uuid}.json"
18
+ Jscov.configuration.coverage_report_dir_path.join(name)
16
19
  end
17
20
  end
18
21
  end
@@ -9,28 +9,8 @@ module Jscov
9
9
  end
10
10
 
11
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, {}, []]
12
+ response = @app.call(env)
13
+ Bless.new(response).result
34
14
  end
35
15
  end
36
16
  end
@@ -1,7 +1,15 @@
1
+ require "capybara"
1
2
  require "jscov"
3
+ require "jscov/test_hooks"
2
4
 
3
5
  RSpec.configure do |config|
4
6
  config.before(:suite) do
5
7
  Jscov.clean!
6
8
  end
9
+
10
+ %i[system feature].each do |type|
11
+ config.after(type: type) do
12
+ Jscov::TestHooks.new(Capybara.current_session).after_example!
13
+ end
14
+ end
7
15
  end
@@ -0,0 +1,46 @@
1
+ require "json"
2
+
3
+ module Jscov
4
+ class TestHooks
5
+ def initialize(session)
6
+ @session = session
7
+ end
8
+
9
+ def after_example!
10
+ return unless selenium?
11
+
12
+ dump_coverage
13
+
14
+ browser_logs.each do |log|
15
+ coverage = parse(log.message)
16
+ if coverage
17
+ Coverage.new(coverage).save
18
+ end
19
+ end
20
+ end
21
+
22
+ def selenium?
23
+ @session.driver.browser.respond_to?(:manage)
24
+ end
25
+
26
+ def dump_coverage
27
+ @session.execute_script <<~JS
28
+ typeof __jscov_dumpCoverage === 'function' && __jscov_dumpCoverage()
29
+ JS
30
+ end
31
+
32
+ def browser_logs
33
+ @session.driver.browser.manage.logs.get(:browser)
34
+ end
35
+
36
+ def parse(message)
37
+ json_string = message[/__jscov" (.*)/, 1]
38
+ return if json_string.nil?
39
+
40
+ json = JSON.parse(json_string)
41
+ JSON.parse(json)
42
+ rescue JSON::ParserError
43
+ nil
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module Jscov
2
- VERSION = '0.2.0'
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jscov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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-10-10 00:00:00.000000000 Z
11
+ date: 2020-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -25,21 +25,21 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: capybara
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
33
+ version: '3.0'
34
+ type: :runtime
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: '0'
40
+ version: '3.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: capybara
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -116,6 +116,7 @@ files:
116
116
  - lib/jscov/coverage.rb
117
117
  - lib/jscov/rack_middleware.rb
118
118
  - lib/jscov/rspec.rb
119
+ - lib/jscov/test_hooks.rb
119
120
  - lib/jscov/version.rb
120
121
  homepage: https://github.com/kzkn/jscov
121
122
  licenses: