Giter VIP home page Giter VIP logo

rspec-snapshot's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rspec-snapshot's Issues

Fails if snapshot_name has forward slashes

I was using rspec-snapshot via this blog post https://www.bearer.com/blog/snapshot-testing-viewcomponents-with-rspec

This started to fail with 2.0.2. The workaround seems easy enough though:

config.after(:each, type: :component, snapshot: true) do |example|
  class_name = example.metadata[:described_class].name.underscore.tr("/", "_")
  test_name = example.metadata[:full_description].tr(example.metadata[:described_class].name, "").tr(" ", "_")
  raise "component snapshot has no content" if page.native.blank?

  expect(page.native).to match_snapshot("#{class_name}-#{test_name}")
end

Expected behaviour

It creates the folders below snapshot like <= 2.0.1 did.

don't write snapshot when negated

Howdy! Beautiful gem.

I would expect

expect("foo").to_not match_snapshot("bar_output")

to never write "bar_output"

Does that make sense? If so I'd be happy to follow-up with a PR

Making the snapshot name optional

In jest, if you can omit the snapshot name; it'll derive it from the example name. Is that behavior supported? If it isn't, I'd be happy to help build that support. Thoughts?

Command to Update Snapshot automatically.

We are using this gem to generate snapshot for our html templates and one thing which annoys me the most is whenever I change anything in my template I have to delete the previous snapshot and re-run the test to generate the snapshot with the new changes.

I was wondering if it is possible to add a feature - Just run one command say update-snapshot with the test path and my snapshot will automatically updated.

Provide configurable serialization

I'm not sure I'll have time to open a PR within the next few days, so I wanted open an issue for discussion. I'll start by listing the desired features as I understand them.

configurable serialization

This could be done globally, perhaps like

RSpec.configure { |c| c.snapshot_serializer = :my_serializer }

or per-expectation like

match_snapshot 'snapshot_file_name', serializer: :my_serializer

This would require a notion of serializer registration:

RSpec::Snapshot.register_serializer :custom_serializer, CustomSnapshotSerializer

default serializer

The default serializer could look like something like this:

class DefaultSerializer
  def dump(object)
    object.to_s
  end
end

This illustrates the serializer API: #dump(Object) : String

built-in serializers

JSON

Ruby ships with the json package in the standard library, so we could use that.

HTML

Nokogiri is a widely-used XML/HTML library, but it is an external dependency. Perhaps it would be worthwhile to extract an rspec-snapshot-html_serializer gem that encapsulates the dependency.


Do you have any opinion on these features? Again, I likely will not have time to work on a pull request until sometime next week. I just wrote this up to remember what I'd like to do.

Project status?

๐Ÿ‘‹ Hello! I wanted to express my gratitude for the wonderful contribution you've made by offering this project to the community.

I've been exploring the issues and releases, and I couldn't help but wonder if the project is still being actively maintained. Would you be able to share some information or update us on its status?

Thank you!

Possible to cut a new release?

Any plans to release a new version of rspec-snapshot to rubygems to include the UPDATE environmental variable and other recent changes?

Issues with Object Comparison

I'm having some tests come back as failures despite reporting that would seem to indicate otherwise.

For example:

  1) AuthToken decodes valid tokens
     Failure/Error: expect(AuthToken.decode(@token)).to match_snapshot("controllers/concerns/auth_token_decode")

       expected: {"user_id"=>1}
            got: {"user_id"=>1}
     # ./spec/api/controllers/concerns/auth_token_spec.rb:13:in `block (2 levels) in <top (required)>'
     # ./spec/rails_helper.rb:68:in `block (2 levels) in <top (required)>'

Coming off the following tests:

require 'rails_helper'

RSpec.describe AuthToken, :type => :request do
  before(:example) do
    @token = AuthToken.encode({user_id: 1},9999)
  end
  
  it "creates new tokens" do
    expect(@token).to match_snapshot("controllers/concerns/auth_token_encode")
  end
  
  it "decodes valid tokens" do
    expect(AuthToken.decode(@token)).to match_snapshot("controllers/concerns/auth_token_decode")
  end
end

It seems like the comparison of objects might be expecting the referential equality?  But I'm not sure why these tests fail when other tests looking at similar responses pass.

Edit: wrapping the snapshot expectation in JSON.generate resolves this issue.

Break old test after add gem

Hi,
I have a big project, and after added rspec-snapshot a lot of tests break. Should the tests run outsite the test context? Seems like mocks are interfering in snapshot process, even if the test don't use snapshot.

describe "TestClass" do

  context "problems" do
    describe "mocking ENV" do
      before do
        allow(ENV).to receive(:[]).with("PARAMETER") { "false" }
      end
      it "it" do
        expect(1).to be 1
      end
    end
    describe "mocking File" do
      before do
        expect(File).to receive(:read).and_return({})
      end
      it "it" do
        File.read("file.rb")
        expect(1).to be 1
      end
    end
  end

result:

RSpec::Mocks::MockExpectationError:  received :[] with unexpected arguments
  expected: ("PARAMETER")
       got: ("UPDATE_SNAPSHOTS")
 Please stub a default value first if message might be received with other args as well. 


  0) TestClass problems mocking ENV it
     Failure/Error: DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure }

      received :[] with unexpected arguments
         expected: ("PARAMETER")
              got: ("UPDATE_SNAPSHOTS")
        Please stub a default value first if message might be received with other args as well. 
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing.rb:7:in `update?'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/recorder.rb:5:in `initialize'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/rspec.rb:10:in `new'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/rspec.rb:10:in `block in included'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/rspec.rb:17:in `block in included'

TypeError: no implicit conversion of Hash into String

  0) TestClass problems mocking File it
     Failure/Error: eval(input)

     TypeError:
       no implicit conversion of Hash into String
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/snapshot.rb:20:in `eval'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/snapshot.rb:20:in `load'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/snapshot.rb:15:in `load_file'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/recorder.rb:21:in `snapshots'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/recorder.rb:122:in `obsolete'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/recorder.rb:70:in `commit'
     # /Users/victorpinto/.rvm/gems/ruby-2.7.1/gems/snapshot_testing-0.3.4/lib/snapshot_testing/rspec.rb:17:in `block in included'

When I remove the gem:

2 examples, 0 failures, 2 passed

Finished in 0.257794 seconds


Randomized with seed 43369

Process finished with exit code 0

Updating snapshots

Hi @yesmeck,

I love this tool, it's really helped my team to build a clean and consistent component system, and ensure there are no regressions with our outputted HTML!

I discovered by running the tests with these matchers that the snapshots are automatically created the first time. However, after looking through the documentation and code, it doesn't look like there is an ability to overwrite the snapshots on demand, if they have changed intentionally. Is this true?

It is a feature I would love, and be interested in proposing a PR for. I don't think rspec allows custom CLI flags, but this could be achieved through an environment variable, which seems typically to be how custom functionality like that is achieved.

Curious to hear your thoughts!

Edit: I'm not even sure if Jest snapshots allows this kind of functionality.

Try to stringify `@actual` during comparison

First off, thanks for writing this helpful library!

I noticed a small inconvenience when trying to snapshot Ruby data structures. For instance, this example fails

RSpec.describe 'snapshots of simple data structures' do
  subject { %w(a simple list) }
  it { should match_snapshot('my_list') }
end

with a confusing message:

 Failure/Error: it { should match_snapshot('my_list') }

       expected: ["a", "simple", "list"]
            got: ["a", "simple", "list"]

At first glance, the expected and actual values look identical, but that's because #failure_message stringifies both values.

Since this matcher implicitly stringifies on the way out (via the call to File#write), what do you think about changing the comparison line from @actual == @expect to something along the lines of @actual.to_s == @expect? I'm happy to write a pull request with associated tests.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.