jscov 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99ef4a868465e2904e7c3630c95b7e65d279156394278ad540e7de000547f1d5
4
- data.tar.gz: c2dd6c216c26c20e5061d76a68ae0c2136ff9832723185a5d089e27c0a842b60
3
+ metadata.gz: f82380fb241d12d3d6eebe7d3e8871b27cabb321616e512b98b5608e59bfb0cc
4
+ data.tar.gz: 45bee86b0495527eb965828c79142572e26c0010e2b983d870cecf6d61cc4e27
5
5
  SHA512:
6
- metadata.gz: 8425216d48ef1a6f01995530d8756f49bfa55a382bbe327d2efb563cde9d3d3f202cbe4d32700bb2c4835d247de9d9628206194fdfb86c1f8d0619946be3fda4
7
- data.tar.gz: '029e8f9733aab260bed599f2133accc86b00db24062fbf421cd44b35730ef0163b4f821e2032c0546d302e33422f6208591c6b0d554b3fa692a9177c218962d1'
6
+ metadata.gz: 22683961b04ade60df6a5a8511cfba794370e81476b55518b049b7ab75d5fba1fa530039cdb8e6aa8d7baba99d5e65ab5e4599d46d01bd3661857b5b34c8d092
7
+ data.tar.gz: 0436e397fc6250f3e3430b0c12a0cd4afa20db53d4596dd47896e59106615dd71b86a64a5700a1f312b5e85cdcc89e080a8813d34c9dc2dc282cf3b6116209c0
data/README.md CHANGED
@@ -64,13 +64,26 @@ Configure selenium to capture the output of `console.log`:
64
64
 
65
65
  ```ruby
66
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' })
67
+ config.before(type: :system) do
68
+ caps = Selenium::WebDriver::Remote::Capabilities.chrome(
69
+ 'chromeOptions' => { w3c: false },
70
+ 'goog:loggingPrefs' => { browser: 'ALL' }
71
+ )
69
72
  driven_by :selenium, using: :headless_chrome, options: { desired_capabilities: caps }
70
73
  end
71
74
  end
72
75
  ```
73
76
 
77
+ And configure rspec to save coverage files after each examples:
78
+
79
+ ```ruby
80
+ RSpec.configure do |config|
81
+ config.after(type: :system) do
82
+ Jscov.save!
83
+ end
84
+ end
85
+ ```
86
+
74
87
  Run `NODE_ENV=test RAILS_ENV=test bin/rails assets:precompile` for generating js codes that applied `istanbul`:
75
88
 
76
89
  ```bash
@@ -84,6 +97,23 @@ The collected coverages can be output as a report using [nyc](https://github.com
84
97
  $ npx nyc report --temp-dir=tmp/jscov
85
98
  ```
86
99
 
100
+ ### Tips
101
+
102
+ Selenium's `logs.get(:browser)` is a destructive method. `jscov` depends on it. If you use it out of `jscov`, it will affect to result of `jscov`, and vice versa.
103
+
104
+ You can pass browser logs to `Jscov.save!` to avoid this issue:
105
+
106
+ ```ruby
107
+ logs = Capybara.current_session.driver.browser.manage.logs.get(:browser)
108
+ Jscov.save!(logs: logs)
109
+ ```
110
+
111
+ If you use multiple capybara sessions, you can pass your capybara sessions to `Jscov.save!` to save coverages that collected on your sessions:
112
+
113
+ ```
114
+ Jscov.save!(session: your_capybara_session)
115
+ ```
116
+
87
117
  ### Vue.js
88
118
 
89
119
  To collect coverage for `.vue` files, you will need to change the configurations.
@@ -1,6 +1,7 @@
1
1
  require "securerandom"
2
2
  require "jscov/configuration"
3
3
  require "jscov/rack_middleware"
4
+ require "jscov/collector"
4
5
 
5
6
  module Jscov
6
7
  class << self
@@ -17,5 +18,10 @@ module Jscov
17
18
  FileUtils.rm_f(Dir.glob(dir_path.join("jscov_*.json")))
18
19
  FileUtils.mkdir_p(dir_path)
19
20
  end
21
+
22
+ def save!(session: nil, logs: nil)
23
+ collector = Collector.new(session, logs)
24
+ collector.coverages.each(&:save)
25
+ end
20
26
  end
21
27
  end
@@ -0,0 +1,48 @@
1
+ require "json"
2
+ require "jscov/coverage"
3
+
4
+ module Jscov
5
+ class Collector
6
+ def initialize(session, logs)
7
+ @session = session || Capybara.current_session
8
+ @logs = logs
9
+ end
10
+
11
+ def coverages
12
+ return [] unless selenium?
13
+
14
+ dump_coverage
15
+
16
+ browser_logs
17
+ .map { |log| parse(log.message) }
18
+ .compact
19
+ .map { |cov| Coverage.new(cov) }
20
+ end
21
+
22
+ def selenium?
23
+ @session.driver.browser.respond_to?(:manage)
24
+ end
25
+
26
+ def dump_coverage
27
+ code = <<~JS
28
+ typeof __jscov_dumpCoverage === 'function' && __jscov_dumpCoverage()
29
+ JS
30
+ @session.execute_script(code)
31
+ end
32
+
33
+ def browser_logs
34
+ logs = @session.driver.browser.manage.logs.get(:browser)
35
+ (@logs || []) + logs
36
+ end
37
+
38
+ def parse(message)
39
+ json_string = message[/__jscov" (.*)/, 1]
40
+ return if json_string.nil?
41
+
42
+ json = JSON.parse(json_string)
43
+ JSON.parse(json)
44
+ rescue JSON::ParserError
45
+ nil
46
+ end
47
+ end
48
+ end
@@ -1,15 +1,8 @@
1
1
  require "capybara"
2
2
  require "jscov"
3
- require "jscov/test_hooks"
4
3
 
5
4
  RSpec.configure do |config|
6
5
  config.before(:suite) do
7
6
  Jscov.clean!
8
7
  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
15
8
  end
@@ -1,3 +1,3 @@
1
1
  module Jscov
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.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.3.0
4
+ version: 0.4.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-18 00:00:00.000000000 Z
11
+ date: 2020-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -112,11 +112,11 @@ files:
112
112
  - Rakefile
113
113
  - lib/jscov.rb
114
114
  - lib/jscov/bless.rb
115
+ - lib/jscov/collector.rb
115
116
  - lib/jscov/configuration.rb
116
117
  - lib/jscov/coverage.rb
117
118
  - lib/jscov/rack_middleware.rb
118
119
  - lib/jscov/rspec.rb
119
- - lib/jscov/test_hooks.rb
120
120
  - lib/jscov/version.rb
121
121
  homepage: https://github.com/kzkn/jscov
122
122
  licenses:
@@ -1,46 +0,0 @@
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