@websolute/faker.ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

ISC license npm version Codecov Coverage CircleCI semantic-release

Faker.ts Logo

Faker.ts

Easy to Use, Powered by Decorators, Faker.js TypeScript Wrapper
Faker.ts is currently in it's alpha version

Installation

Install the package alongside faker.js and @types/faker peer dependencies:

npm i -D @websolute/faker.ts@1.0.0-alpha.1 faker @types/faker

Playground

We have create a REPL Playground where you can see Faker.ts in action!

Usage

Here is the simplest usage of Faker.ts:

import { Fixture, FixtureFactory } from 'faker.ts';

class Dog {
  @Fixture(faker => faker.name.firstName())
  readonly name: string;
  
  @Fixture()
  readonly birthday: Date;

  @Fixture()
  readonly goodPoints: number;
}

const result = FixtureFactory.create<Dog>(Dog);

A more complex example:

import { Fixture, FixtureFactory } from 'faker.ts';

class Person {
  @Fixture(faker => faker.name.firstName())
  readonly name: string;
  
  @Fixture()
  readonly birthday: Date;

  @Fixture(faker => faker.internet.email())
  readonly email: string;

  @Fixture({ type: Dog })
  readonly dog: Dog;
}

const result = FixtureFactory.create<Person>(Person);

There are more options available to you in using @Fixture decorator and also the FixtureFactory as well

Jump to the full documentation and explore the full API

Motivation

For those of you who are unfamiliar with faker.js, it is an old library written with pure JavaScript (it also has types in @types/faker), which is used to "generate massive amounts of fake data in the browser and Node".

Fake data is usually needed for testing purposes, to assist in the development process itself, and sometimes, also for the purpose of demonstrations and training. To generate the data, use the faker library directly, such as: faker.internet.email() and that, of course, will generate a random email address from a pre-made database.

To create full fake objects, you need to place them in a literal object and use for loop. Faker.ts provides an easy and simple solution to an annoying and inconvenient problem that allows you to set "fixtures" as metadata on the department itself. This also allows the use of interfaces and, among other things, the enforcement of contracts.

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements

faker.js

Package Sidebar

Install

npm i @websolute/faker.ts

Weekly Downloads

4

Version

1.0.0

License

MIT

Unpacked Size

51.6 kB

Total Files

69

Last publish

Collaborators

  • omerico