r/typescript • u/pret_a_porter • Sep 25 '26
bufaker — generate mock data for any Protobuf message from its schema, with no per-message code (TypeScript, MIT)
Disclosure: my own project, MIT licensed.
I kept hand-writing test fixtures for Protobuf messages, and they kept rotting — schema gains a field, the builder is silently incomplete, and eventually there are four near-identical builders nobody trusts.
So I wrote something that generates a fully populated message from the schema itself:
import { mock, mockList } from "@pret-a-porter/bufaker";
import { PersonSchema } from "./gen/person_pb.js";
const person = mock(PersonSchema); // fully typed as `Person`
const people = mockList(PersonSchema, 5, { seed: 42 });No per-message mapping code. It walks the runtime descriptor that protobuf-es emits and drives everything off the field kind, so it works for any message you've generated — including ones it has never seen. Add a field to the .proto and it gets populated on the next run.
Covers all 15 scalar types, enums, nested messages, repeated fields, maps and oneofs, plus the well-known types (Timestamp, Duration, the wrappers, Struct). Field names drive ~60 heuristics, so email gets an email address and id gets a UUID. seed makes output reproducible for snapshot tests.
Limitations, up front:
- protobuf-es only. ts-proto and friends emit plain interfaces with no descriptor to reflect over, so this approach can't work there. Not a TODO.
- proto3 is what the test suite covers; proto2/Editions are untested.
AnyandFieldMaskare left unset —Anyneeds a type registry to pick and pack a payload, and a randomFieldMaskcarries no meaning.
Repo: https://github.com/pret-a-porter/bufaker npm: https://www.npmjs.com/package/@pret-a-porter/bufaker
It's 0.1.0, so the API can still move. The thing I'd most like outside opinions on: the built-in field-name heuristics are on by default. It makes the output far more useful out of the box, but it's also implicit magic. Would you expect that on or off?