Skip to content

rodmax/factory-t

Repository files navigation

factory-t

factory-t

build/tests npm SonarQualityStatus

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

npm install factory-t

Usage

To get started you need to:

  • Design your data type
interface UserDto {
    id: number;
    email: string;
    phone: string | null;
    profile: {
        avatarUrl: string;
        language: 'EN' | 'RU';
        statusString?: string;
    };
}
  • Import the right tools
import { factoryT, fields } from 'factory-t';
  • Create a factory (or a set of factories)
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
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 and/or unit test 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 - 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.

License

MIT

About

Library for building JavaScript/TypeScript objects... with strong typing in mind

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 6