Jest for Go

github.com/malcolmston/jest

Jest-style fluent assertions and mocking for Go.

 GitHubports jestjs/jest

A standard-library-only Go framework that brings the expressive, fluent feel of JavaScript's Jest to Go tests, layered directly on top of the standard testing package. The entry point is a generic Expect[T] that returns a fluent Matcher[T] with a full family of matchers — ToBe, ToEqual, ToContain, ToHaveLen, ToMatch, ToBeCloseTo, numeric ordering and ToPanic — each invertible with .Not(). On top of that sit configurable Mocks (Return/ReturnValues plus CallCount/CalledWith/LastCall inspection), type-safe Fn0/Fn1/Fn2 and Spy helpers, and Describe/It/BeforeEach/AfterEach that run over t.Run through a small TestReporter interface satisfied by *testing.T. No cgo, no third-party dependencies — assertions plug straight into go test.

Install

shell
$ go get github.com/malcolmston/jest

Quick start

main.go
import "github.com/malcolmston/jest"

func TestMathAndMocks(t *testing.T) {
	jest.Expect(t, 2+2).ToBe(4)
	jest.Expect(t, []int{1, 2, 3}).ToContain(2)
	jest.Expect(t, "hello world").ToMatch("world")
	jest.Expect(t, 5).Not().ToBe(6)

	fn, mock := jest.Fn1[int, string]("stringify")
	mock.Return("hi")
	jest.Expect(t, fn(7)).ToBe("hi")
	jest.Expect(t, mock.CalledWith(7)).ToBeTrue()
}

Features

  • Generic fluent assertions — Expect[T] returns a Matcher[T] that reports through a TestReporter satisfied by *testing.T
  • Equality & nil — ToBe (shallow ==), ToEqual (deep equal with a diff), ToBeNil, ToBeTrue/ToBeFalse
  • Containment & shape — ToContain (substring / slice element / map key), ToHaveLen, ToMatch (regexp)
  • Numbers — ToBeCloseTo with an epsilon plus ToBeGreaterThan/ToBeGreaterThanOrEqual/ToBeLessThan/ToBeLessThanOrEqual
  • Panics & negation — ToPanic/ToThrow assert a func() panics, and any matcher inverts with .Not()
  • Mocks — NewMock with Return/ReturnValues and CallCount/Called/CalledWith/LastCall/NthCall/Calls/Reset inspection
  • Type-safe function doubles — Fn0/Fn1/Fn2 mock functions and Spy0/Spy1/Spy2 that record while delegating to a real impl
  • Test organization — Describe/It (alias Test) over t.Run with scoped, composable BeforeEach/AfterEach hooks
  • Zero dependencies — pure Go standard library, fully integrated with go test, -run filtering and -v output
Jest