fix!: use stable key ordering for snapshots with maps #190
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Snapshots containing maps are unstable because iteration order of a map's keys is not guaranteed. This leads to test instability, where instability increases with the size of the map included in the snapshot.
Stability can be introduced by using the option of the go-spew library that sorts map keys before serialization (the
SortKeys
option). TheSortKeys
option only deals with keys that are native types, or types that implementStringer
orerror
, but this covers a significant proportion of the uses ofmap
.Breaking change: This change turns the
SortKeys
option on for all snapshots, potentially invalidating snapshots that already exist. In reality though, given the instability of maps in snapshots currently, it's unlikely any user of the library will be affected.Further development
There is another option:
SpewKeys
. This extends the function ofSortKeys
to other types by first usinggo-spew
to serialize the type to a string. In my opinion, this is likely to either be (a) something a library user might want to opt into, and (b) a less-likely scenario. For this reason, I've left that option as a potential future development.See also
go-spew
configuration reference.