From a79133f622b807bce4455ed18e845c469b063af7 Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Thu, 8 Dec 2022 17:02:16 +0100 Subject: [PATCH 1/2] do not write on negated matcher --- lib/rspec/snapshot/matchers/match_snapshot.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/rspec/snapshot/matchers/match_snapshot.rb b/lib/rspec/snapshot/matchers/match_snapshot.rb index 921a83c..91bc038 100644 --- a/lib/rspec/snapshot/matchers/match_snapshot.rb +++ b/lib/rspec/snapshot/matchers/match_snapshot.rb @@ -43,6 +43,13 @@ def initialize(metadata, snapshot_name, config) FileUtils.mkdir_p(File.dirname(@snapshot_path)) end + def does_not_match?(actual) + @actual = serialize(actual) + @expected = read_snapshot + + @actual != @expected + end + def matches?(actual) @actual = serialize(actual) From aae4a1875990b956bda6096174d881c386051ae4 Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Thu, 8 Dec 2022 17:04:11 +0100 Subject: [PATCH 2/2] don't write on negated match --- spec/rspec/snapshot/matchers_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/rspec/snapshot/matchers_spec.rb b/spec/rspec/snapshot/matchers_spec.rb index 48a5dea..ad3e537 100644 --- a/spec/rspec/snapshot/matchers_spec.rb +++ b/spec/rspec/snapshot/matchers_spec.rb @@ -284,6 +284,32 @@ def dump(object) end end + context 'and a snapshot file exists' do + let(:original_snapshot_value) { 'foo' } + let(:updated_snapshot_value) { 'bar' } + let(:snapshot_name) { 'update_existing_snapshot' } + let(:snapshot_path) do + current_directory_path.join('__snapshots__', + "#{snapshot_name}.snap") + end + + before do + file = File.new(snapshot_path, 'w+') + file.write(original_snapshot_value) + file.close + # rubocop:disable RSpec/ExpectInHook + expect(updated_snapshot_value).to_not match_snapshot(snapshot_name) + # rubocop:enable RSpec/ExpectInHook + file = File.new(snapshot_path) + @actual = file.read + file.close + end + + it 'ignores the snapshot and updates it to the current value' do + expect(@actual).to_not eq(updated_snapshot_value) + end + end + context 'and a snapshot file does not exist' do let(:snapshot_value) { 'foo' } let(:snapshot_name) { 'update_non_existing_snapshot' }