Skip to content

Commit

Permalink
chore: print diff after approvals failure
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 3, 2021
1 parent 7d8ccca commit 0d77fc2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
18 changes: 13 additions & 5 deletions spec/spec_helper.rb
Expand Up @@ -12,18 +12,13 @@
require 'sucker_punch/testing/inline'
require 'webmock/rspec'
require 'pact_broker/policies'
require 'approvals/rspec'

Dir.glob("./spec/support/**/*.rb") { |file| require file }

WebMock.disable_net_connect!(allow_localhost: true)

I18n.config.enforce_available_locales = false

Approvals.configure do |c|
c.approvals_path = 'spec/fixtures/approvals/'
end

RSpec.configure do | config |
config.before :each do
PactBroker.reset_configuration
Expand Down Expand Up @@ -53,6 +48,19 @@
config.example_status_persistence_file_path = "./spec/examples.txt"
config.filter_run_excluding skip: true

config.after(:each) do | example, something |
if example.exception.is_a?(Approvals::ApprovalError)
require "pact/support"
parts = example.exception.message.split('"')
received_file = parts[1]
approved_file = parts[3]
received_hash = JSON.parse(File.read(received_file))
approved_hash = JSON.parse(File.read(approved_file))
diff = Pact::Matchers.diff(approved_hash, received_hash)
puts Pact::Matchers::UnixDiffFormatter.call(diff)
end
end

def app
PactBroker::API
end
Expand Down
24 changes: 24 additions & 0 deletions spec/support/approvals.rb
@@ -0,0 +1,24 @@
require 'approvals/rspec'
require 'pact/support'

Approvals.configure do |c|
c.approvals_path = 'spec/fixtures/approvals/'
end

def print_diff(exception)
parts = exception.message.split('"')
received_file = parts[1]
approved_file = parts[3]
received_hash = JSON.parse(File.read(received_file))
approved_hash = JSON.parse(File.read(approved_file))
diff = Pact::Matchers.diff(approved_hash, received_hash)
puts Pact::Matchers::UnixDiffFormatter.call(diff)
end

RSpec.configure do | config |
config.after(:each) do | example |
if example.exception.is_a?(Approvals::ApprovalError)
print_diff(example.exception)
end
end
end

0 comments on commit 0d77fc2

Please sign in to comment.