Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@ func (v verifyOptions) WithRegexScrubber(scrubber *regexp.Regexp, replacer strin
return v
}

// WithCustomScrubber allows you to 'scrub' dynamic data such as timestamps within your test input
// using a custom function and replace it with a static placeholder
func (v verifyOptions) WithCustomScrubber(scrubFunc func(s, r string) string, replacer string) verifyOptions {
v.scrubbers = append(v.scrubbers, func(s string) string {
return scrubFunc(s, replacer)
})
return v
}

// WithExtension overrides the default file extension (.txt) for approval files.
func (v verifyOptions) WithExtension(extension string) verifyOptions {
v.extWithDot = extension
Expand Down
11 changes: 11 additions & 0 deletions scrubber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ func TestVerifyAllWithRegexScrubber(t *testing.T) {
xs := []string{"Christopher", "Llewellyn"}
approvals.VerifyAll(t, "uppercase", xs, func(x interface{}) string { return fmt.Sprintf("%s => %s", x, strings.ToUpper(x.(string))) }, opts)
}

func scrubFunc(s, replacer string) string {
return regexp.MustCompile("Hello").ReplaceAllString(s, replacer)
}

func TestVerifyJSONBytesWithCustomScrubber(t *testing.T) {
opts := approvals.Options().WithCustomScrubber(scrubFunc, "Hi")

jb := []byte("{ \"Greeting\": \"Hello\" }")
approvals.VerifyJSONBytes(t, jb, opts)
}