# @cats-cradle/faker-factory

> FakerFactory creates fakes of classes using class-validator decorators for testing purposes.

Latest version **3.1.0** (published 2024-06-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @cats-cradle/faker-factory
pnpm add @cats-cradle/faker-factory
yarn add @cats-cradle/faker-factory
bun add @cats-cradle/faker-factory
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2024-06-27 |
| First published | 2023-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 15 |
| Unpacked size | 34.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Keywords | faker-factory, faker, json-schema, class-validator, mocks, testing |

## Links

- npm: https://www.npmjs.com/package/@cats-cradle/faker-factory
- Repository: https://github.com/hxtree/cats-cradle
- Homepage: https://github.com/hxtree/cats-cradle#readme
- Issues: https://github.com/hxtree/cats-cradle/issues
- npm.io page: https://npm.io/package/@cats-cradle/faker-factory

## Dependencies (15)

- [uuid](https://npm.io/package/uuid.md) ~9.0.1
- [lodash](https://npm.io/package/lodash.md) ~4.17.21
- [currency.js](https://npm.io/package/currency.js.md) ~2.0.4
- [json-schema](https://npm.io/package/json-schema.md) ~0.4.0
- [openapi3-ts](https://npm.io/package/openapi3-ts.md) ~3.1.2
- [@types/lodash](https://npm.io/package/@types/lodash.md) ~4.14.202
- [@faker-js/faker](https://npm.io/package/@faker-js/faker.md) ~7.6.0
- [class-validator](https://npm.io/package/class-validator.md) 0.13.2
- [@types/validator](https://npm.io/package/@types/validator.md) ~13.7.10
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) 0.1.13
- [class-transformer](https://npm.io/package/class-transformer.md) 0.5.1
- [json-schema-faker](https://npm.io/package/json-schema-faker.md) ~0.5.0-rcv.46
- [@cats-cradle/base-nodejs](https://npm.io/package/@cats-cradle/base-nodejs.md) 3.0.0
- [class-validator-jsonschema](https://npm.io/package/class-validator-jsonschema.md) 3.1.1
- [@cats-cradle/validation-schemas](https://npm.io/package/@cats-cradle/validation-schemas.md) 2.1.0

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [@jsdevlab/valibot](https://npm.io/package/@jsdevlab/valibot.md) — 0 weekly downloads
- [n8n-nodes-ecom-adspy-tco](https://npm.io/package/n8n-nodes-ecom-adspy-tco.md) — 0 weekly downloads
- [mokapi-storybook-addon](https://npm.io/package/mokapi-storybook-addon.md) — 0 weekly downloads

## Recent versions

- 3.1.0 (latest) — 2024-06-27
- 1.2.11 — 2024-03-05
- 1.2.10 — 2024-02-04
- 1.2.9 — 2023-12-28
- 1.2.8 — 2023-12-12
- 1.2.7 — 2023-12-03
- 1.2.6 — 2023-11-13
- 1.2.5 — 2023-11-05
- 1.2.4 — 2023-11-04
- 1.2.3 — 2023-11-03
- 1.2.2 — 2023-11-02
- 1.2.1 — 2023-11-02
- 1.2.0 — 2023-11-01
- 1.1.0 — 2023-10-28
- 1.0.4 — 2023-10-04
- … 8 more at https://npm.io/package/@cats-cradle/faker-factory/versions

## README

# @cats-cradle/faker-factory

FakerFactory creates fakes of classes using class-validator decorators for
testing purposes.

It creates a faker object by first inferring each property's type based the
class-validator decorator. Next, it changes the object to a schema then it
generates fake data for each property. Lastly, the faked object is instantiated
and returned.

## Getting Started

Create a fake by calling the FakerFactory.

```typescript
import { FakerFactory } from '@cats-cradle/faker-factory';
import { Person } from './person.ts';

const fakerPerson = await FakerFactory.create<Person>(Person, { passed: true });
```

```typescript
// person.ts or any class that uses class-validator decorators
import {
  IsString,
  IsInt,
  IsBoolean,
  IsUUID,
  IsCurrency,
  Min,
  ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';

export class Person {
  @IsUuid();
  public id: string;

  @IsString()
  @Length(0,50)
  @IsOptional()
  public firstName: string;

  @IsCurrency()
  public currency: string;

  @IsInt()
  @Min(0)
  public visits: number;

  @IsBoolean()
  public passed: boolean;

  @IsOptional()
  @ValidateNested({ each: true })
  @Type(() => Person)
  public guests: Person[];
}
```

```ts
// result
// console.log(fakerPerson);
{
  "id": "4cb85e06-1060-4bed-8224-14ec39e0dfa9",
  "firstName": "irure in",
  "currency": "887.56",
  "visits": 24235,
  "passed": true
}
```

## Settings

FakerFactory accepts a third optional parameter for settings. Settings are used
to change the way the FakerFactory works. Adjusting the settings can be useful
for when writing unit tests on optional fields.

| Property    | Description                            | Possible Values        |
| ----------- | -------------------------------------- | ---------------------- |
| probability | probability for faking optional fields | 0, ... 0.5, ... 1      |
| optionals   | whether to fake optional fields        | true, false, undefined |

```typescript
const Person = await FakerFactory.create<Person>(
  Person,
  {},
  { optionals: true },
);
```

## Opinions

In typescript object data can be automatically faked based on either: the
property name, property typehint, separate schema, or the decorator schema.

Property names should remain somewhat consistent and fakes can be generated
based on them, this is how [Intermock](https://github.com/google/intermock)
works. However, it is unreasonable to maintain such a many-to-many relationship
for faking purposes.

Generating values based on property typehinting is less then ideal currently in
typescript due to compiled code being type agnostic and the special tsc
requirement to add such a feature.

Generating fakes purely based on separate schema such as
[AJV](https://www.npmjs.com/package/ajv),
[JIO](https://www.npmjs.com/package/joi), or
[validatorjs](https://www.npmjs.com/package/validatorjs) works but it is less
readable than a purely decorator based approach.

A class-validator decorators and schema base approach was chosen for the
following reasons:

- Decorators are already present when using class-validation.
- Faker data should not just be fake, it should be valid and meet
  class-validator requirements.
- If a class-validator requires a string length faker data should meet that
  requirement.
- It is easy to create new class-validator decorators and extend functionality.

## References

- [F.I.R.S.T Principles of Testing](https://medium.com/@tasdikrahman/f-i-r-s-t-principles-of-testing-1a497acda8d6)
- [Fake vs Mock](https://medium.com/@june.pravin/mocking-is-not-practical-use-fakes-e30cc6eaaf4e)
- [Faking vs Mocking vs Stubbing](https://www.educative.io/answers/what-is-faking-vs-mocking-vs-stubbinghttps://www.educative.io/answers/what-is-faking-vs-mocking-vs-stubbing)
- [fakerJs](https://fakerjs.dev/api/date.html#date)
- [class-transformer](https://github.com/typestack/class-transformer/tree/master)
- [class-validator](https://github.com/typestack/class-validator)
- [class-validator jsonschema](https://github.com/epiphone/class-validator-jsonschema)

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