levinmr / rspec-snapshot Goto Github PK
View Code? Open in Web Editor NEWRSpec snapshot testing.
License: MIT License
RSpec snapshot testing.
License: MIT License
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
It creates the folders below snapshot like <= 2.0.1 did.
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
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?
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.
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.
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
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
Ruby ships with the json
package in the standard library, so we could use that.
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.
๐ 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!
Such feature would greatly ease debugging of changed snapshots :)
Any plans to release a new version of rspec-snapshot to rubygems to include the UPDATE environmental variable and other recent changes?
do they need to be manually crafted?
can we generate them from an initial test run ?
I'm not seeing anything that suggest how should one begin?!
thanks
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.
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
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.
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.
Test CI job sometimes fails with a handful of failed specs. Re-running causes it to pass. Determine the cause and implement a fix
in Jest, snapshots are not automatically written in CI, thus the test will fail if there is no snapshot. It feels like this should work the same otherwise tests are likely to slip by when snapshot names are changed.
Currently the failure message inspects the matcher rather than providing a nice diff showing expected vs actual.
TODO: update with an example
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.