-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Making the snapshot name optional #6
Comments
PR is welcome. |
Is this being worked on? If not, I'll probably add this early next week. |
@tncbbthositg Welcome PR! |
Seconded! |
This would be a great feature. Is |
Agree that this would be a useful feature. The way jest does it is to use the spec file name as the snapshot name and then treat the snapshot file like a hash using the example descriptions as the keys. Will put this on the roadmap for the next version |
vcr has a common pattern for generating names of cassettes from the rspec example. # probably already have this in the code
example = RSpec.current_example
# get the description (name) or the scoped id (like 1:2:4:8)
path_data = [example.metadata[:description] || example.metadata[:scoped_id]]
parent = example.example_group
base_path = ""
while parent != RSpec::ExampleGroups
base_path = File.dirname(parent.file_path.gsub("./spec/", ""))
path_data << parent.metadata[:description]
parent = parent.module_parent
end
path_data << base_path if base_path.present?
# Need to do this differently since it's a Rails thing
path_data = path_data.map { ActiveStorage::Filename.new(_1).sanitized }
# path_data is ['renders_component', 'when rating is > 0', 'StarRatingComponent', 'components']
name = path_data.reverse.join("/")
expect(rendered_component).to match_snapshot(name) Will output a file to
although I have all of my snapshots going to
There's also another common vcr-ism that is probably nicer for the default folder setup: name =
RSpec.current_example
.metadata[:full_description]
.split(/\s+/, 2)
.join("/")
.underscore.tr(".", "/")
.gsub(%r{[^\w/]+}, "_")
.gsub(%r{/$}, "") that results in
This one can have collisions though, for example if you had a context "when rating is >= 0" and "when rating is < 0" the names would be the same because it's aggressive about stripping characters regardless of safety. This issue could be fixed by sanitizing the name like |
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?
The text was updated successfully, but these errors were encountered: