# factory-t

> TypeScript library for building data objects. With strong typings in mind

Latest version **2.0.0** (published 2026-10-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install factory-t
pnpm add factory-t
yarn add factory-t
bun add factory-t
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2026-10-12 |
| First published | 2019-04-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=24.0.0 |
| Dependencies | 0 |
| Unpacked size | 60 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Maxim Rodionov |
| Maintainers | rodmax |
| Keywords | TypeScript, factory, unit-test |

## Links

- npm: https://www.npmjs.com/package/factory-t
- Repository: https://github.com/rodmax/factory-t
- Issues: https://github.com/rodmax/factory-t/issues
- npm.io page: https://npm.io/package/factory-t

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2026-10-12
- 0.3.0-beta.0 (beta) — 2020-05-18
- 1.1.0 — 2025-12-25
- 1.0.0 — 2025-04-07
- 0.3.1 — 2021-09-17
- 0.3.0 — 2020-09-29
- 0.3.0-beta.1 — 2020-09-29
- 0.2.1 — 2019-09-19
- 0.2.0 — 2019-08-23
- 0.1.5 — 2019-06-24
- 0.1.4 — 2019-04-25
- 0.1.3 — 2019-04-16
- 0.1.2 — 2019-04-16

## README

# factory-t

![factory-t](docs/factory-t-poster.png)

[![build/tests](https://github.com/rodmax/factory-t/actions/workflows/ci.yaml/badge.svg)](https://github.com/rodmax/factory-t/actions/workflows/ci.yaml?query=branch%3Amain)
[![npm](https://img.shields.io/npm/v/factory-t)](https://www.npmjs.com/package/factory-t)
[![SonarQualityStatus](https://sonarcloud.io/api/project_badges/measure?project=rodmax_factory-t&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=rodmax_factory-t)

> pronounced like `ˈfakt(ə)rē tē`

TypeScript library for building data objects.

Useful for unit tests and mocking data during development.

With strong typing in mind.

## Goals

- Provide a factory for building data objects based on a predefined config
- The factory instance should **strongly depend** on the target TypeScript
  interface, so if the interface changes, the factory should not be built
  until it is synced with the interface changes
- All kinds of API "sugar" are appreciated, but with typings in mind

## Install

```bash
npm install factory-t
```

## Usage

To get started you need to:

- Design your data type

<!-- embedme ./src/tests/readme-snippets.test.ts#L6-L15-->

```ts
interface UserDto {
    id: number;
    email: string;
    phone: string | null;
    profile: {
        avatarUrl: string;
        language: 'EN' | 'RU';
        statusString?: string;
    };
}
```

- Import the right tools

<!-- embedme ./src/tests/readme-snippets.test.ts#L1-L1-->

```ts
import { factoryT, fields } from 'factory-t';
```

- Create a factory (or a set of factories)

<!-- embedme ./src/tests/readme-snippets.test.ts#L17-L28-->

```ts
const profileFactory = factoryT<UserDto['profile']>({
    avatarUrl: 'my.site/avatar',
    language: fields.sequence(['EN', 'RU']),
    statusString: fields.optional('sleeping'),
});

const userFactory = factoryT<UserDto>({
    id: fields.index(),
    email: (ctx) => `user-${ctx.index}@g.com`,
    phone: fields.nullable('+127788'),
    profile: (ctx) => profileFactory.item({ avatarUrl: `/avatars/${ctx.index}` }),
});
```

- Use it to build objects

<!-- embedme ./src/tests/readme-snippets.test.ts#L30-L39-->

```ts
expect(userFactory.item({ phone: null })).toStrictEqual({
    id: 1,
    email: 'user-1@g.com',
    phone: null, // overridden by the partial passed to item(...)
    profile: {
        avatarUrl: '/avatars/1', // overridden by userFactory using its index
        language: 'EN',
        statusString: 'sleeping',
    },
});
```

See [tutorial](./docs/tutorial.md) and/or
[unit test](./src/tests/factory-t.test.ts) for details.

## Status

I use this library in all my current projects (mostly for unit tests).

In my opinion, it has a simple implementation, so if you like its API, you can
try using it too.

## Similar projects

### [rosie](https://github.com/rosiejs/rosie) - nice library, good intuitive API

If you write in JavaScript, it should solve all your needs.

The main drawback is typings. This library has `@types/rosie`, but it uses a
"builder" pattern which does not allow strong type checking between the factory
and the target interface.

## Contributing

Pull requests are welcome. For major changes, please open an issue first to
discuss what you would like to change.

Please make sure to update tests as appropriate.

For more details see the [development guide](DEVELOPMENT.md).

## License

[MIT](https://choosealicense.com/licenses/mit/)

---
_Source: https://npm.io/package/factory-t · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
