lighthouse-matchers 1.2.0 → 1.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: 1b73401322398ad659f111555923e60f2859c90e1e24893c209686842340de06
4
- data.tar.gz: 2fec0776316b961004e8254ab49f374dc56eff5096a3336d2216294b10cbe13c
3
+ metadata.gz: e47d9c8b9ac4ff8fe209317f9054fafd3c5d7c81f1f241cb61be6035940ea50a
4
+ data.tar.gz: 6627e960001bad68703a9a0d5eb2a20691f97f137b8293fcd24f70813c1df6d9
5
5
  SHA512:
6
- metadata.gz: f847e1a748e9097df8a7345e60f3249f15ae9c3031885a756c9ce2a6bf582032d4de281c2f2b6c088a73baca86a7158f3101f637f9c376298b665848d1f9f7c2
7
- data.tar.gz: f461dd4cc3b146228986c833857421a9861b9b65d0b1095b90f10dae40bed454009c83fc5b1dadb60ca246a43768fec984226933fd980abe81aa990681c3c715
6
+ metadata.gz: 2628e449c6d825aea0eed45bfbd2a3e9f3c63d925495033003f4c3eae649a6a4686f72ca90e388e61131aacd893a491733ed23b4762698b21bfd8c31ef7cbb04
7
+ data.tar.gz: 6da50b69d8d31d21ab669c3b7e1de4d74f731af703d9bae63ee8082836fa6be0a92d53bf1a64bef0bcf21c2598edaef58f6b153f3c716da3fe3a45f753f78a26
data/CHANGELOG.md CHANGED
@@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.3.0] - 2025-08-13
10
+ ### Added
11
+ - 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))
12
+
13
+ ### Changed
14
+ - Prefix warnings with the full example description ([#69](https://github.com/ackama/lighthouse-matchers/pull/69))
15
+
9
16
  ## [1.2.0] - 2024-07-13
10
17
  ### Changed
11
18
  - 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.3.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.3.9)
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
@@ -103,6 +103,9 @@ All configuration keys are accessible against the `Lighthouse::Matchers` object.
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
105
  * **`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`).
106
+ * **`results_directory`:** Directory to write lighthouse results on failure
107
+ * Defaults to `<RSpec.configuration.default_path>/lighthouse` if `RSpec` is defined, otherwise a temporary directory prefixed with `lighthouse-matchers-`
108
+ * For Rails applications, we recommend setting this to `Rails.root.join('/tmp/lighthouse')` in your `rails_helper.rb`
106
109
 
107
110
  ## Compatibility
108
111
 
@@ -29,6 +29,10 @@ class AuditService
29
29
  results['runWarnings']
30
30
  end
31
31
 
32
+ def results
33
+ @results ||= JSON.parse(output)
34
+ end
35
+
32
36
  private
33
37
 
34
38
  def category
@@ -54,8 +58,4 @@ class AuditService
54
58
  def output
55
59
  @output ||= @runner.call("#{@cmd} #{opts}")
56
60
  end
57
-
58
- def results
59
- @results ||= JSON.parse(output)
60
- end
61
61
  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.3.0'
6
6
  end
7
7
  end
@@ -9,7 +9,7 @@ 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
12
+ attr_writer :minimum_score, :lighthouse_cli, :runner, :chrome_flags, :results_directory
13
13
  attr_accessor :remote_debugging_port
14
14
 
15
15
  def 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