Support ignoring fields when serializing
See original GitHub issueIs your feature request related to a problem? Please describe.
When asserting on snapshots support filtering out properties that should not be matched on. This would allow deterministic snapshots when matching on objects that generate realtime values in tests without needing to mock.
Describe the solution you’d like
def test_my_dict(snapshot):
my_dict = {
"match_me": True,
"do_not_match_me": time.time(),
"nested_do_not_match": {
"do_not_match_me": time.time(),
},
}
assert my_dict == snapshot(exclude=("do_not_match_me",))
# name: test_my_dict
<class 'dict'> {
'match_me': True,
'nested_do_not_match': <class 'dict'> {
},
}
---
Describe alternatives you’ve considered
This is already possible but is cumbersome to implement, and cannot easily be extended to other serialization types.
class OmitDataSerializer(DataSerializer):
@classmethod
def __filter_data(cls, data) -> Dict[str, Any]:
return {
key: data[key]
for key in data
if key not in ("do_not_match_me",)
}
@classmethod
def serialize_dict(cls, data, **kwargs) -> str:
return super().serialize_dict(cls.__filter_data(data), **kwargs)
class OmitDataExtension(AmberSnapshotExtension):
def serialize(self, data):
return OmitDataSerializer.serialize(data)
def test_my_dict(snapshot):
my_dict = {
"match_me": True,
"do_not_match_me": time.time(),
"nested_do_not_match": {
"do_not_match_me": time.time(),
},
}
assert my_dict == snapshot(extension=OmitDataExtension)
Additional context
- Supported by Jest https://jestjs.io/docs/en/snapshot-testing#property-matchers
- Much requested feature, see counterpart https://github.com/syrusakbary/snapshottest/issues/21
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top Results From Across the Web
How to exclude field from class serialization in runtime?
So when you declare a variable as transient, it should be ignored by ObjectOutputStream. Make sure that you use the transient keyword and...
Read more >Jackson Ignore Properties on Marshalling
This tutorial will show how to ignore certain fields when serializing an object to JSON using Jackson 2.x.
Read more >How to ignore properties with System.Text.Json
To ignore read-only fields when serializing fields, use the JsonSerializerOptions.IgnoreReadOnlyFields global setting.
Read more >Spring Boot — Dynamically ignore fields while serializing ...
Spring Boot — Dynamically ignore fields while serializing Java Object to JSON · Properties which are to be ignored is supplied to SimpleBeanPropertyFilter....
Read more >Gson – Exclude or Ignore Fields
By default, Gson would exclude a field from serialization and deserialization – both, if we simply mark the field as transient. Remember that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s neat – it could definitely work for some cases (though there are a few places where we generate v5 UUIDs off other external IDs coming in). To clarify, though, we’re not blocked on this at all; we just run our custom “mask nondeterministic values” function on our data before asserting on the snapshot.
Thoughts on
include
andexclude
instead ofignore
?We could support strings (literal property matchers) and predicates