Harness
@ghostry/harness wraps a Jest-compatible test runner (bun, vitest, jest, mocha, or node:test). It gives you the same API on every runner, lets integrations like fabricator's hook into each test, and offers features of its own, such as attest (assertions that also narrow TypeScript types). See its docs for everything it does.
Fabricator's integration uses it for one thing: each test's data comes from the test's identity, meaning its describe names, its own name, and its .each row. You don't pass anything into the test body or make up a salt.
Setup
One shared module sets up both, and every test file imports from it:
// test/setup.ts
import { initialize as initializeFabricator } from "@ghostry/fabricator";
import { integration as fabricatorIntegration } from "@ghostry/fabricator/harnessing";
import { initialize as initializeHarness } from "@ghostry/harness";
import * as framework from "bun:test";
export const fabricator = initializeFabricator({
clock: new Date("2024-01-01T00:00:00Z"),
});
export const { describe, it, expect } = initializeHarness({
framework,
integrations: [fabricatorIntegration(fabricator)],
});// test/user.test.ts
import { describe, expect, fabricator, it } from "./setup";
const { T, fabricate } = fabricator;
const User = T.object({
name: T.string.whereby({ length: { min: 1, max: 25 } }),
joined: T.date.past,
});
describe("users", () => {
it("has a name", () => {
const user = fabricate(User);
expect(user.name.length).toBeGreaterThan(0);
});
});Nothing in the test mentions a salt. Running it twice fabricates the same user. Renaming the test, or moving it under a different describe, fabricates a different one.
Pin a fixed Date
The clock in the setup module matters. Without it, the instance uses the time of the initialize() call, so every run of the suite gets different data, and you can only get back to a failing run if someone logged its context.clock. See The clock is the entropy.
Every test inherits a pinned Date unchanged, so the whole suite shares one realistic "now", and only each test's identity changes its data.
clock: "derived" also removes the dependency on the time, but it derives "now" from the salt, and every test has a different salt. So every test, and every beforeAll, gets its own "now", which can be any date in the representable range. A beforeAll and the tests under it then disagree about the present. The integration does this on purpose: "derived" means the salt alone determines everything, and a separate clock per test is the result. Pin a Date unless that's what you want.
What each test's salt is
Every test body runs inside instance.wrap(...), with its identity layered onto the instance salt:
[...instanceSalt, "test", ...describeNames, testName]; // an ordinary test
[...instanceSalt, "test", ...describeNames, testName, "2"]; // row 2 of an .each
[...instanceSalt, "suite", ...describeNames, ""]; // beforeAll / afterAllBecause the identity is added to the salt instead of replacing it, initialize({ salt }) or FABRICATOR_SALT still changes the salt for the whole suite at once.
Each wrap also starts a fresh construction counter, so the first build in every test is that test's construction 0. fabricate's cache starts empty in every test too, so a User defined at the top of the file doesn't carry over from one test to the next. What other tests built, or whether they ran at all, has no effect. .only, name filters, sharding, and .concurrent can't change a test's data.
Why this uses wrap
The reproducibility guide recommends fork over wrap in ordinary code, because a call through a fork always uses that fork's configuration. This integration is the exception the guide describes. It has to apply a per-test configuration to a test body it didn't write and can't pass a parameter into. Applying it automatically is the only way to reach fabricate(User) inside your test, so wrap is the right tool here. context.fabricator is still there wherever you'd rather be explicit.
The instance you pass to integration(...) doesn't have to be the one initialize() returned. You can fork an application-wide configuration and use that fork as your root, and it works the same way:
const app = initializeFabricator({ clock: new Date("2024-01-01T00:00:00Z") });
export const fabricator = app.fork({ salt: layer("testing") });
export const { describe, it, expect } = initializeHarness({
framework,
integrations: [fabricatorIntegration(fabricator)],
});Each test's wrap is opened on the instance in effect, which here is the fork, since nothing else wraps it. So a fabricate or Fabricator destructured from that fork picks up each test's identity, and the per-test salt gets "testing" before the identity. Forks of that fork are not affected.
When something else does wrap it, the per-test salt is added to that scope instead of replacing it. The extern extension does this around every testing block. The integrated instance is an ancestor of that scope, so it is still affected. A fork of the integrated instance is not.
Reaching the scope
The integration adds one key to the test context: fabricator, the instance for this test. That same instance also applies automatically for the whole test body, so both of these draw from it:
it("builds two users", ({ fabricator: scope }) => {
fabricate(User); // ambient
scope.fabricate(User); // explicit, same scope
});The two share one cache, so the second call draws the test's second user from the same Fabricator, not a second copy of the first. new Fabricator(User) and new scope.Fabricator(User) share one construction counter the same way. Use context.fabricator to pass the scope to a helper, or to read scope.salt for a log message. To make per-test data with its own extra salt, fork the scope: scope.fork(...), or fabricator.context.scope().fork(...) if you don't have the context. A fork of the integrated instance itself is not per test. See A module-level fork is not per test.
The automatic scope lasts through await on Node, Bun, and Deno. On a runtime without node:async_hooks, an async test body throws SynchronousStackError whichever way it builds. See Runtime support.
Hooks
@ghostry/harness runs beforeEach and afterEach inside the test itself, so they share the test's scope. The hook's context.fabricator is the same as the body's, and constructions in a beforeEach and in the body share one counter.
beforeAll and afterAll get a suite scope instead, with the "suite" salt shown above. It stays the same between runs and differs from every test under it. A beforeAll and an afterAll in the same describe share that scope on purpose. To give one its own, call fabricator.wrap({ salt: layer("teardown") }, ...) inside the hook.
Running hooks inside the test has costs: a hook failure is reported against the test, and hook time counts against the test's timeout. Those come from the harness. See Hooks.
Potential surprises
A module-level fork is not per test
The per-test wrap applies to the instance passed to integration(...) and its ancestors, not to forks of it. A fork made with fabricator.fork({ salt: layer("A") }) at the top of a file uses the same salt in every test, and its construction counter keeps counting across tests. So the values a test gets depend on which tests ran before it. You'll see tests that fail together as a suite but pass under .only. Fork context.fabricator (or fabricator.context.scope()) inside the test instead.
Same path, same data
A test's identity is its path, not its file. Two tests with the same describe names and test name get the same data, even in different files. This usually happens with a shared helper that registers the same test names from several files. Each test is still deterministic, so nothing breaks, but it can be surprising. Give them different names.
The same goes for describe.each. Put a placeholder from the row in its title (describe.each(rows)("case $name", ...)), or the tests in two rows share one path.
A construction salt wants layer(...)
A plain construction salt replaces the salt in effect, including the test's identity:
it("a", () => new Fabricator(User, { salt: "fixed" }).fabricate());
it("b", () => new Fabricator(User, { salt: "fixed" }).fabricate()); // same user as "a"Both use the same salt and the same pinned clock, and since the counter restarts per test, both are construction 0. Everything that determines the data is the same, so the user is the same. { salt: layer("fixed") } adds to the test's identity instead and keeps them apart.
Retries stop suppressing flakes
A retried test has the same path and gets the same data, so a failure caused by fabricated data fails the same way on every attempt. A retry no longer hides it; it keeps showing the real problem. That is intended, but it changes what retries are good for.
Checking your runner
@ghostry/harness/conformance registers a suite that checks that your runner behaves the way the harness expects. Put it in its own test file, and see Conformance for what it checks:
import { conformance } from "@ghostry/harness/conformance";
import * as framework from "bun:test";
conformance(framework);