lighthouse-matchers 1.2.0 → 1.4.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: 1b73401322398ad659f111555923e60f2859c90e1e24893c209686842340de06
4
- data.tar.gz: 2fec0776316b961004e8254ab49f374dc56eff5096a3336d2216294b10cbe13c
3
+ metadata.gz: 13b0c2fb40335271d04162ba9302b8f8601a294d6c8c26b6e904322013daf227
4
+ data.tar.gz: 891fa3772857cb424f555b49985347867b2ab89560e5fa3c5992fe689e8d49ed
5
5
  SHA512:
6
- metadata.gz: f847e1a748e9097df8a7345e60f3249f15ae9c3031885a756c9ce2a6bf582032d4de281c2f2b6c088a73baca86a7158f3101f637f9c376298b665848d1f9f7c2
7
- data.tar.gz: f461dd4cc3b146228986c833857421a9861b9b65d0b1095b90f10dae40bed454009c83fc5b1dadb60ca246a43768fec984226933fd980abe81aa990681c3c715
6
+ metadata.gz: 58d574c6e7bce9e98fac696113867807615e996d1c564520c276a5eb296fa76d83e6c1714d63a6a6fae4008549c896215e87562535dbcdd7a5194a9bc592e7e2
7
+ data.tar.gz: f24d482030024f6bb8a20d9b679cdcad500b892097e7d6b95fa1c6ea0b0d8c2c6c248d0a1ab7913f903a0886c5cec06aa8bc79871b01bb43f1eb7fcb630f0f85
data/CHANGELOG.md CHANGED
@@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.4.0] - 2025-09-17
10
+ ### Added
11
+ - Added config options for "preset" and "form-factor" so those parameters can be passed to the `lighthouse` command ([#71](https://github.com/ackama/lighthouse-matchers/pull/71))
12
+
13
+ ## [1.3.0] - 2025-08-13
14
+ ### Added
15
+ - Write audit results to disk on failure so they can be viewed using the browser viewer ([#68](https://github.com/ackama/lighthouse-matchers/pull/68))
16
+
17
+ ### Changed
18
+ - Prefix warnings with the full example description ([#69](https://github.com/ackama/lighthouse-matchers/pull/69))
19
+
9
20
  ## [1.2.0] - 2024-07-13
10
21
  ### Changed
11
22
  - Raise an explict error when the audit category is not found ([#56](https://github.com/ackama/lighthouse-matchers/pull/56))
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lighthouse-matchers (1.2.0)
4
+ lighthouse-matchers (1.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -14,8 +14,7 @@ GEM
14
14
  rainbow (3.0.0)
15
15
  rake (13.0.1)
16
16
  regexp_parser (2.2.0)
17
- rexml (3.2.8)
18
- strscan (>= 3.0.9)
17
+ rexml (3.4.2)
19
18
  rspec (3.8.0)
20
19
  rspec-core (~> 3.8.0)
21
20
  rspec-expectations (~> 3.8.0)
@@ -43,9 +42,8 @@ GEM
43
42
  rubocop-ast (1.15.1)
44
43
  parser (>= 3.0.1.1)
45
44
  ruby-progressbar (1.11.0)
46
- strscan (3.1.0)
47
45
  unicode-display_width (2.1.0)
48
- webrick (1.7.0)
46
+ webrick (1.8.2)
49
47
 
50
48
  PLATFORMS
51
49
  ruby
data/README.md CHANGED
@@ -102,7 +102,15 @@ All configuration keys are accessible against the `Lighthouse::Matchers` object.
102
102
  for the CLI. This setting can be used if the Lighthouse tool is installed in a non-standard location.
103
103
  * **`minimum_score`:** The default minimum score that audits must meet for the matcher to pass.
104
104
  The default value of this configuration setting is '100' - e.g. audits must fully comply to pass.
105
+ * **`preset`:** The built-in configuration that will be used by Lighthouse CLI if defined
106
+ Options: "perf", "experimental", "desktop"
107
+ * **`form_factor`:** If defined, this value changes how performance metrics are measured and whether mobile only checks are performed
108
+ If you are intending to run lighthouse to only test the desktop version of your site, it is recommended that you use preset instead.
109
+ Options: "desktop", "mobile"
105
110
  * **`chrome_flags`:** Any additional flags that should be passed to Chrome when Lighthouse launches a browser instance. As an example, running Lighthouse in Docker requires the normal headless Chrome flags (`--headless`, `--no-sandbox`) for Chrome to successfully start. Chrome flags can either be specified as an array (`["headless", "no-sandbox"]`) or as a string (`--headless --no-sandbox`).
111
+ * **`results_directory`:** Directory to write lighthouse results on failure
112
+ * Defaults to `<RSpec.configuration.default_path>/lighthouse` if `RSpec` is defined, otherwise a temporary directory prefixed with `lighthouse-matchers-`
113
+ * For Rails applications, we recommend setting this to `Rails.root.join('/tmp/lighthouse')` in your `rails_helper.rb`
106
114
 
107
115
  ## Compatibility
108
116
 
@@ -15,6 +15,8 @@ class AuditService
15
15
  @runner = Lighthouse::Matchers.runner
16
16
  @cmd = Lighthouse::Matchers.lighthouse_cli
17
17
  @chrome_flags = Lighthouse::Matchers.chrome_flags
18
+ @preset = Lighthouse::Matchers.preset
19
+ @form_factor = Lighthouse::Matchers.form_factor
18
20
  end
19
21
 
20
22
  def passing_score?
@@ -29,6 +31,10 @@ class AuditService
29
31
  results['runWarnings']
30
32
  end
31
33
 
34
+ def results
35
+ @results ||= JSON.parse(output)
36
+ end
37
+
32
38
  private
33
39
 
34
40
  def category
@@ -48,14 +54,12 @@ class AuditService
48
54
  builder << " --only-categories=#{@audit}"
49
55
  builder << " --port=#{@port}" if @port
50
56
  builder << " --chrome-flags='#{@chrome_flags}'" if @chrome_flags
57
+ builder << " --preset=#{@preset}" if @preset
58
+ builder << " --form-factor=#{@form_factor}" if @form_factor
51
59
  end.strip
52
60
  end
53
61
 
54
62
  def output
55
63
  @output ||= @runner.call("#{@cmd} #{opts}")
56
64
  end
57
-
58
- def results
59
- @results ||= JSON.parse(output)
60
- end
61
65
  end
@@ -4,28 +4,35 @@ require 'rspec/expectations'
4
4
  require 'lighthouse/matchers'
5
5
  require 'lighthouse/audit_service'
6
6
  require 'json'
7
+ require 'digest'
8
+ require 'fileutils'
7
9
 
8
- RSpec::Matchers.define :pass_lighthouse_audit do |audit, args = {}|
10
+ RSpec::Matchers.define :pass_lighthouse_audit do |audit, args = {}| # rubocop:disable Metrics/BlockLength
9
11
  score ||= args.fetch(:score, Lighthouse::Matchers.minimum_score)
10
12
 
11
13
  match do |target|
12
- audit_service = AuditService.new(url(target), audit, score)
14
+ @audit_service = AuditService.new(url(target), audit, score)
13
15
 
14
- audit_service.run_warnings.each do |warning|
16
+ @audit_service.run_warnings.each do |warning|
15
17
  RSpec.configuration.reporter.message(
16
- "#{RSpec.current_example.location}: [lighthouse] #{warning}"
18
+ "#{RSpec.current_example.full_description}: [lighthouse] #{warning}"
17
19
  )
18
20
  end
19
21
 
20
- @measured_score = audit_service.measured_score
22
+ @measured_score = @audit_service.measured_score
21
23
 
22
- audit_service.passing_score?
24
+ @audit_service.passing_score?
23
25
  end
24
26
 
25
27
  failure_message do |target|
26
28
  <<~FAIL
27
29
  expected #{url(target)} to pass Lighthouse #{audit} audit
28
30
  with a minimum score of #{score}, but only scored #{@measured_score.to_i}
31
+
32
+ Full report:
33
+ #{save_audit_results}
34
+
35
+ To view this report, load this file into https://googlechrome.github.io/lighthouse/viewer/
29
36
  FAIL
30
37
  end
31
38
 
@@ -34,4 +41,15 @@ RSpec::Matchers.define :pass_lighthouse_audit do |audit, args = {}|
34
41
  def url(target)
35
42
  target.respond_to?(:current_url) ? target.current_url : target
36
43
  end
44
+
45
+ def save_audit_results
46
+ body = JSON.pretty_generate(@audit_service.results)
47
+
48
+ path = File.join(Lighthouse::Matchers.results_directory, "#{Digest::SHA1.hexdigest(body)}.json")
49
+
50
+ FileUtils.mkdir_p(Lighthouse::Matchers.results_directory)
51
+ File.write(path, body)
52
+
53
+ path
54
+ end
37
55
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lighthouse
4
4
  module Matchers
5
- VERSION = '1.2.0'
5
+ VERSION = '1.4.0'
6
6
  end
7
7
  end
@@ -9,8 +9,8 @@ module Lighthouse
9
9
  module Matchers # rubocop:disable Style/Documentation
10
10
  class Error < StandardError; end
11
11
  class << self
12
- attr_writer :minimum_score, :lighthouse_cli, :runner, :chrome_flags
13
- attr_accessor :remote_debugging_port
12
+ attr_writer :minimum_score, :lighthouse_cli, :runner, :chrome_flags, :results_directory
13
+ attr_accessor :remote_debugging_port, :preset, :form_factor
14
14
 
15
15
  def minimum_score
16
16
  @minimum_score ||= default_minimum_score
@@ -31,6 +31,14 @@ module Lighthouse
31
31
  @chrome_flags.map { |f| "--#{f}" }.join(' ')
32
32
  end
33
33
 
34
+ def results_directory
35
+ @results_directory ||= if defined?(RSpec)
36
+ File.join(RSpec.configuration.default_path, 'lighthouse')
37
+ else
38
+ Dir.mktmpdir('lighthouse-matchers-')
39
+ end
40
+ end
41
+
34
42
  private
35
43
 
36
44
  def guess_lighthouse_cli