1.1.0 • Published 4 years ago
fake-class v1.1.0
fake-class
fake-class instantiates objects with fake data.
Requirement
- TypeScript version 3.3 or higher
You have enabled the following settings in
tsconfig.json
:"emitDecoratorMetadata": true, "experimentalDecorators": true,
Install
Install the npm package:
npm install fake-class --save
Usage
Basic Usage
import { fakeClass, FakableClass, FakableField } from "fake-class";
@FakableClass()
class User {
public id?: number;
@FakableField(() => "takkyuuplayer@example.com")
public email?: string;
public active: boolean = true;
}
const user = fakeClass(User);
console.log(user); // User {active: true, email: 'takkyuuplayer@example.com' }
Field Relation
You can set orders for fields to be resolved earlier than the others.
The default order is Number.MAX_VALUE
.
Useful to relate faked fields.
@FakableClass()
class User {
@FakableField(() => 5, {
order: 0
})
public id?: number;
@FakableField((user: User) => `test-${user.id}@example.com`)
public email?: string;
}
const user = fakeClass(User);
console.log(user); // User {id: 5, email: 'test-5@example.com' }
More Examples
See more examples.