Skip to content

Commit 976ab2b

Browse files
committed
Add options for passing a string formatter
1 parent ce661cc commit 976ab2b

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ RSpec.configure do |config|
9898
# Set this value to put all snapshots in a fixed directory
9999
config.snapshot_dir = "spec/fixtures/snapshots"
100100

101+
# Defaults to not applying any additional formatting
102+
#
103+
# Set this value to use a custom formatter for when `expect` receives a string
104+
config.snapshot_formatter = MyFavoriteFormatter
105+
101106
# Defaults to using the awesome_print gem to serialize values for snapshots
102107
#
103108
# Set this value to use a custom snapshot serializer
@@ -114,18 +119,31 @@ You can pass custom serializers to `rspec-snapshot` if you prefer. Pass a serial
114119
matcher as a config option:
115120

116121
```ruby
122+
# Set a custom string formatter for all tests
123+
RSpec.configure do |config|
124+
config.snapshot_formatter = MyCoolGeneralFormatter
125+
end
126+
117127
# Set a custom serializer for all tests
118128
RSpec.configure do |config|
119129
config.snapshot_serializer = MyCoolGeneralSerializer
120130
end
121131

122-
# Set a custom serializer for this specific test
132+
# Set a custom formatter for this specific test
123133
expect(html_response).to(
124-
match_snapshot('html_response', { snapshot_serializer: MyAwesomeHTMLSerializer })
134+
match_snapshot('html_response', { snapshot_formatter: MyAwesomeFormatter })
135+
)
136+
137+
# Set a custom serializer for this specific test
138+
expect(result_object).to(
139+
match_snapshot('result_object', { snapshot_serializer: MyAwesomeSerializer })
125140
)
126141
```
127142

128-
Serializer classes are required to have one instance method `dump` which takes
143+
Formatters are required to have a single `call` method that takes the raw string value
144+
and returns a formatted string. If you want, you can use a proc or lambda.
145+
146+
Serializer classes are required to have one instance method `dump`, which takes
129147
the value to be serialized and returns a string.
130148

131149
## Migration

lib/rspec/snapshot/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Configuration; end
1010
def self.initialize_configuration(config)
1111
config.add_setting :snapshot_dir, default: :relative
1212

13+
config.add_setting :snapshot_formatter, default: nil
14+
1315
config.add_setting :snapshot_serializer, default: nil
1416
end
1517

lib/rspec/snapshot/matchers/match_snapshot.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@ def initialize(metadata, snapshot_name, config)
1414
@metadata = metadata
1515
@snapshot_name = snapshot_name
1616
@config = config
17+
@formatter = formatter
1718
@serializer = serializer_class.new
1819
@snapshot_path = File.join(snapshot_dir, "#{@snapshot_name}.snap")
1920
create_snapshot_dir
2021
end
2122

23+
private def formatter
24+
if @config[:snapshot_formatter]
25+
@config[:snapshot_formatter]
26+
elsif RSpec.configuration.snapshot_formatter
27+
RSpec.configuration.snapshot_formatter
28+
else
29+
->(value) { value }
30+
end
31+
2232
private def serializer_class
2333
if @config[:snapshot_serializer]
2434
@config[:snapshot_serializer]
@@ -44,7 +54,7 @@ def initialize(metadata, snapshot_name, config)
4454
end
4555

4656
def matches?(actual)
47-
@actual = serialize(actual)
57+
@actual = serialize(format(actual))
4858

4959
write_snapshot
5060

@@ -56,6 +66,14 @@ def matches?(actual)
5666
# === is the method called when matching an argument
5767
alias === matches?
5868

69+
private def format(value)
70+
if value.is_a?(String)
71+
@formatter.call(value)
72+
else
73+
value
74+
end
75+
end
76+
5977
private def serialize(value)
6078
if value.is_a?(String)
6179
value

0 commit comments

Comments
 (0)