New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/customerio/go-approval-tests

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/customerio/go-approval-tests

  • v0.0.0-20241212163817-4f0fe712d0b6
  • Source
  • Go
  • Socket score

Version published
Created
Source

Fork of go-approval-tests

This fork incorporates https://github.com/approvals/go-approval-tests/pull/41 , which has not been accepted yet by upstream. Once/if that PR is accepted, and merged, users of this fork can go back to using the original go-approval-tests.

This fork renames the module to customerio/go-approval-tests. This avoids the use of replace directives in go.mod, which for modules that depend on other modules using replace.


ApprovalTests.go

ApprovalTests for go

GoDoc Go Report Card Coverage Status Build and Test

Golden master Verification Library

ApprovalTests allows for easy testing of larger objects, strings and anything else that can be saved to a file (images, sounds, csv, etc...)

Examples

Basic string verification

func TestHelloWorld(t *testing.T) {
	approvals.VerifyString(t, "Hello World!")
}

Store approved files in testdata subfolder

Some people prefer to store their approved files in a subfolder "testdata" instead of in the same folder as the production code. To configure this, add a call to UseFolder to your TestMain:

func TestMain(m *testing.M) {
	approvals.UseFolder("testdata")
	os.Exit(m.Run())
}

Accept the changes in the output

If you have made changes to the output and want to accept them, you can use the AcceptChanges function:

func TestMain(m *testing.M) {
	approvals.AcceptChanges(true)
}

A good pattern is to use a command line flag to control this behavior, such as -u for update:

var update = flag.Bool("u", false, "update .approved files")

func TestMain(m *testing.M) {
	flag.Parse()
	approvals.AcceptChanges(*update)
	os.Exit(m.Run())
}

In Project

Note: ApprovalTests uses approvals to test itself. Therefore there are many examples in the code itself.

JSON

VerifyJSONBytes - Simple Formatting for easy comparison. Also uses the .json file extension

func TestVerifyJSON(t *testing.T) {
	jsonb := []byte("{ \"foo\": \"bar\", \"age\": 42, \"bark\": \"woof\" }")
	VerifyJSONBytes(t, jsonb)
}

Matches file: approvals_test.TestVerifyJSON.received.json

{
  "age": 42,
  "bark": "woof",
  "foo": "bar"
}

Reporters

ApprovalTests becomes much more powerful with reporters. Reporters launch programs on failure to help you understand, fix and approve results.

You can make your own easily, here's an example You can also declare which one to use. Either at the

Method level

r := UseReporter(reporters.NewIntelliJ())
defer r.Close()

Test Level

func TestMain(m *testing.M) {
	r := UseReporter(reporters.NewBeyondCompareReporter())
	defer r.Close()
	UseFolder("testdata")

	os.Exit(m.Run())
}

FAQs

Package last updated on 12 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc