Giter Site home page Giter Site logo

rodmax / factory-t Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 0.0 991 KB

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

License: MIT License

JavaScript 6.47% TypeScript 89.10% Shell 4.44%
typescript factory mock-data testing

factory-t's Introduction

factory-t


build/tests David (path) npm codecov

Quality Gate Status Bugs Technical Debt Code Smells

Maintainability

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 factory for building data objects based on predefined config
  • Factory instance should strongly depends from target TypeScript interface, so if interface will changed factory should not be built until syncing with interface changes
  • All kind of API "sugar" appreciated but with typings in mind

Install

npm install factory-t

Usage

To start you need

  • Design your data type
interface UserDto {
    id: number;
    email: string;
    phone: string | null;
    profile: {
        avatarUrl: string;
        language: 'EN' | 'RU';
        statusString?: string;
    };
}
  • Import right tools
import { factoryT, fields } from 'factory-t';
  • create factory (or bunch 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}` }),
});
  • and use it to build objects
expect(userFactory.item({ phone: null })).toStrictEqual({
    id: 1,
    email: '[email protected]',
    phone: null, // override by passed partial to item(...)
    profile: {
        avatarUrl: '/avatars/1', // override by userFactory using its index
        language: 'EN',
        statusString: 'sleeping',
    },
});

See tutorial or/and unit test for details

Status

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

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

Similar projects

rosie - nice library, good intuitive API.

if you write on JavaScript, it should solve all your needs.

The main drawback is typings. This library has @types/rosie but it uses "builder" pattern which not allows to strong type checking between factory and 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 development guide

License

MIT

factory-t's People

Contributors

dependabot[bot] avatar greenkeeper[bot] avatar renovate-bot avatar renovate[bot] avatar rodmax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

factory-t's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: .github/renovate.json
Error type: The renovate configuration file contains some invalid settings
Message: Configuration option 'ignorePaths' should be a list (Array)

Optional properties cannot be added with partial param unless defined in the factory

Hi @rodmax,

First, I wanted to thank you for this great library. Lightweight, with strong typings, and it has covered all my use cases so far. Awesome job!

I found an issue when working with optional properties, though. Usually, optional properties are not included in the factory definition (that's why they are optional, right?). However, at this point, it's not possible to add a value for an optional property using the partial param of .item(..) for example.

The only workaround I found is to set the property in the factory definition as undefined. But IMHO, I think that defies the purpose of an optional property, and it's also a little cumbersome to add undefined for all optional properties to avoid this issue.

Here's an example in a small playground I created: https://codesandbox.io/s/factory-t-playground-r9ueq?file=/src/index.ts

Let me know if this makes sense to you. Also, I'll be happy to help out with a PR if you point me in the right direction.

Cheers!

Dependency Dashboard

This issue provides visibility into Renovate updates and their statuses. Learn more

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • chore(deps): update dependency semver-regex to 3.1.3 [security]
  • chore(deps): update dependency tmpl to 1.0.5 [security]

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.


  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.