Jest for Go
Jest-style fluent assertions and mocking for Go.
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
$ go get github.com/malcolmston/jestQuick start
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 aMatcher[T]that reports through aTestReportersatisfied 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 —
ToBeCloseTowith an epsilon plusToBeGreaterThan/ToBeGreaterThanOrEqual/ToBeLessThan/ToBeLessThanOrEqual - Panics & negation —
ToPanic/ToThrowassert afunc()panics, and any matcher inverts with.Not() - Mocks —
NewMockwithReturn/ReturnValuesandCallCount/Called/CalledWith/LastCall/NthCall/Calls/Resetinspection - Type-safe function doubles —
Fn0/Fn1/Fn2mock functions andSpy0/Spy1/Spy2that record while delegating to a real impl - Test organization —
Describe/It(aliasTest) overt.Runwith scoped, composableBeforeEach/AfterEachhooks - Zero dependencies — pure Go standard library, fully integrated with
go test,-runfiltering and-voutput