Registry / testing / factory.ts

factory.ts

JSON →
library1.4.2jsnpmunverified

factory.ts is a TypeScript library designed for generating test data based on predefined interfaces or types, drawing inspiration from Ruby's `factory_bot` and JavaScript's `rosie`. As of its current stable version, 1.4.2, the library provides a robust, strongly-typed approach to creating data factories. Releases typically involve dependency updates and minor feature enhancements, as seen with recent additions like self-derived values and pipeline implementations. Its core differentiator lies in its deep integration with TypeScript, enabling type-safe factory definitions, sequence-number based value generation (`Factory.each`), and composition through factory extension and derived value functions (`withDerivationN`). This ensures that test data strictly adheres to the defined types, reducing runtime errors in test environments. It is primarily used as a development dependency for creating predictable and customizable test fixtures.

npm install factory.ts
INSTALL
IMPORT
SIG · FACTORY.TS
F
factory.ts
testingjavascriptv1.4.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Factory
✓ import * as Factory from "factory.ts";
✗ import Factory from "factory.ts";
`factory.ts` exports its primary functionality as a namespace named `Factory`, which contains modules like `Sync` and `Async`, and helpers like `each`. Direct default imports will fail.
makeFactory
✓ import * as Factory from "factory.ts"; Factory.Sync.makeFactory(...);
✗ import { makeFactory } from 'factory.ts';
The `makeFactory` function for synchronous factories is located within the `Sync` module of the `Factory` namespace (`Factory.Sync.makeFactory`). It is not directly exported from the top-level package.
each
✓ import * as Factory from "factory.ts"; Factory.each(...);
✗ import { each } from 'factory.ts';
The `each` helper, used for sequence-based value generation, is directly available on the top-level `Factory` namespace for convenience. It can also be accessed via `Factory.Sync.each`.

Demonstrates creating a synchronous factory, building single instances with overrides, generating lists, and extending a factory to derive properties from others.

import * as Factory from "factory.ts"; interface Person { id: number; firstName: string; lastName: string; fullName: string; age: number; } // Create a basic synchronous factory for the Person interface const personFactory = Factory.Sync.makeFactory<Person>({ id: Factory.each((i) => i + 1), // Generate sequential IDs starting from 1 firstName: "John", lastName: "Doe", fullName: "John Doe", // Default full name age: Factory.each((i) => 20 + (i % 10)), // Age cycles between 20 and 29 }); // Build a single person with default values const defaultPerson = personFactory.build(); console.log("Default Person:", defaultPerson); // Example: { id: 1, firstName: 'John', lastName: 'Doe', fullName: 'John Doe', age: 20 } // Build a person overriding some properties const janeSmith = personFactory.build({ firstName: "Jane", lastName: "Smith" }); console.log("Jane Smith (overridden):", janeSmith); // Example: { id: 2, firstName: 'Jane', lastName: 'Smith', fullName: 'John Doe', age: 21 } // Build a list of people const threePeople = personFactory.buildList(3, { lastName: "Public" }); console.log("Three Publics:", threePeople); /* Example: [ { id: 3, firstName: 'John', lastName: 'Public', fullName: 'John Doe', age: 22 }, { id: 4, firstName: 'John', lastName: 'Public', fullName: 'John Doe', age: 23 }, { id: 5, firstName: 'John', lastName: 'Public', fullName: 'John Doe', age: 24 } ] */ // Demonstrate extending a factory for derived values const autoFullNameFactory = personFactory.withDerivation2( ["firstName", "lastName"], "fullName", (fName, lName) => `${fName} ${lName}` ); const derivedPerson = autoFullNameFactory.build({ firstName: "Derived", lastName: "Name" }); console.log("Derived Full Name Person:", derivedPerson); // Example: { id: 1, firstName: 'Derived', lastName: 'Name', fullName: 'Derived Name', age: 20 }
Debug
Known issues
breakingThe `RecPartial<>` generic type in `factory.ts` was updated in v1.2.0 to short-circuit on `unknown` types, preventing recursive checks into `unknown` properties. This change fixes issues where `null` or other primitives were incorrectly handled when attempting to set values for `unknown` properties.
fix
Review existing factory definitions and test cases that involve setting `null` or `unknown` values on properties, ensuring they align with the new type behavior.
affects: >=1.2.0
gotchaDerived values currently require using specific `withDerivation1` through `withDerivation5` methods, rather than a single overloaded `withDerivation` function. This is a TypeScript limitation that requires choosing the correct arity function (`withDerivationN`) based on the number of dependent properties.
fix
Ensure you call the correct `withDerivationN` function (e.g., `withDerivation2` for two dependencies) corresponding to the number of dependent keys provided.
affects: *
gotchaWhile `factory.ts` includes TypeScript types, using CommonJS `require()` syntax (`const Factory = require('factory.ts');`) can lead to unexpected type issues or module resolution problems in modern TypeScript projects configured for ESM. The library is primarily designed for ESM consumption.
fix
Prefer `import * as Factory from 'factory.ts';` for consistent behavior and full type support in TypeScript/ESM environments.
affects: *
gotchaThis library is primarily intended for generating test data and should typically be installed as a `devDependency` (`npm install --save-dev factory.ts` or `yarn add -D factory.ts`). Including it as a production dependency unnecessarily increases bundle size and build times for deployable artifacts.
fix
Ensure `factory.ts` is listed under `devDependencies` in your `package.json`.
affects: *
Errors
Common errors & fixes
TypeError: (0, factory_ts_1.default) is not a function
Attempting to import `Factory` as a default export (`import Factory from 'factory.ts';`) when it is exported as a namespace.
fix
Use a namespace import: `import * as Factory from 'factory.ts';`
Property 'makeFactory' does not exist on type 'typeof import("factory.ts")'.
Trying to access `makeFactory` directly on the `Factory` namespace, when it is nested under `Factory.Sync` (or `Factory.Async`).
fix
Access `makeFactory` via the `Sync` module: `Factory.Sync.makeFactory(...)`
Argument of type '(...)' is not assignable to parameter of type 'never'.
Using the incorrect `withDerivationN` function (e.g., `withDerivation1`) for the number of dependent properties specified (e.g., two properties).
fix
Use the `withDerivationN` function that matches the number of dependent properties you are providing (e.g., `withDerivation2` for two dependencies).
Upgrade
Version history
1.4.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources