franiglesias / golden Goto Github PK
View Code? Open in Web Editor NEWGolang Library for snapshot testing
License: MIT License
Golang Library for snapshot testing
License: MIT License
Describe the bug
Not exactly a bug, but the new BetterDiffReported should separate lines.
Manage not deterministic data using scrubbers.
Scrubbers can work using regexp to locate specific info and replace with a static representation. It could be something like "removed for testing", or maybe a valid example of data, but I thing it should be marked some way. This can be a problem if we want to use the snapshot as an example.
We should be able to pass any number of scrubbers.
We should be able to ask for predefined scrubbers, because there are lots of common cases.
The place for scrubbing should be the Normalizer.
t.Name()
provides a good default for snapshot naming after the test name, but...
API
Golden.UseSnapshot(name string) Golden
Combinatorial testing requires to create a simple interface that can be used in multiple situations.
golden.GoldenMaster(t, function, ...values)
It could be good to create the snapshot file as an array of json objects. This could make easier for users to understand the snapshot and review the results.
Provide a way to force the approval of a snapshot before it can be used as matching criteria.
In "snapshot mode", a snapshot that is created for first time is supposed to represent the behaviour of the subject under test.
In "approval mode", a snapshot that is created for first time has to wait to be approved by a human actor before being used as matching criteria.
First, you execute the test in "approval mode", so test will never pass.
golden.ToApprove(t, subject)
Once you are happy with the output, back to verify
golden.Verify(t, subject)
golden.ToApprove(t, subject)
This will kind of a decorator around Verify. It will set a flag so the system can decide if it needs to compare snapshot and subject. Snapshot creation has to be forced even if previous snapshot exists.
A reminder to approve the snapshot can be added to the report.
Is your feature request related to a problem? Please describe.
Type assertions are annoying.
Describe the solution you'd like
Make things more statically typed & easier to use. Also reduce number of places to change when you add/remove/change parameters.
Describe alternatives you've considered
I'm using this helper function which does type assertions for me. Would be nice to have something like that built into the library:
func toGoldenMasterFn(fn interface{}) func(args ...any) any {
return func(args ...any) any {
fnVal := reflect.ValueOf(fn)
if fnVal.Kind() != reflect.Func {
panic("wrapFunctionWithAnyArgs: provided interface is not a function")
}
fnType := fnVal.Type()
if fnType.IsVariadic() {
panic("wrapFunctionWithAnyArgs: variadic functions are not supported")
}
if len(args) != fnType.NumIn() {
panic(fmt.Sprintf("wrapFunctionWithAnyArgs: incorrect number of arguments. Expected %d, got %d", fnType.NumIn(), len(args)))
}
in := make([]reflect.Value, len(args))
for i, arg := range args {
in[i] = reflect.ValueOf(arg)
}
results := fnVal.Call(in)
if len(results) != 1 {
panic("wrapFunctionWithAnyArgs: function does not return exactly one result")
}
return results[0].Interface()
}
}
// Usage:
func TestGoldenMaster(t *testing.T) {
// define the wrapper function
f := toGoldenMasterFn(func(title, part string, span int) any {
return Border(title, part, span)
})
// define the input values to combine
titles := []any{"Example 1", "Example long enough", "Another thing"}
parts := []any{"-", "=", "*", "#"}
times := []any{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// run the test
golden.Master(t, f, golden.Combine(titles, parts, times))
}
Additional context
Another option, maybe even better one - have a set of generic functions for accepting N generic paramters, like:
golang.MasterOf2[T1, T2](t *testing.T, fn func(a T1, b T2) any, params1 []T1, params2 []T2)
golang.MasterOf3[T1, T2, T3](t *testing.T, fn func(a T1, b T2, c T3) any, params1 []T1, params2 []T2, params3 []T3)
// make a bunch more of these to cover all realistic use-cases...
This looks a bit goofy, but in practice it's not that bad - similar pattern is widely used in Elm, for example, and it's just fine https://package.elm-lang.org/packages/elm/json/latest/Json-Decode#map
With this approach, example above could look like:
// Usage:
func TestGoldenMaster(t *testing.T) {
// define the wrapper function
f := func(title, part string, span int) any {
return Border(title, part, span)
})
// define the input values to combine
titles := []any{"Example 1", "Example long enough", "Another thing"}
parts := []any{"-", "=", "*", "#"}
times := []any{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// run the test
golden.MasterOf3(t, f, titles, parts, times)
}
Which is a bit cleaner, and in addition to that you can't swap parameters of different types by accident.
Add a reminder to approve the test when executing in Approval mode
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.