Generate mocks, stubs using fakers with your Entity Settings
- CAUTION: Check the library name: typeorm-faker, NOT typeorm-faker-'js'.
I found a MALICIOUS USER,
A DISGUSTING ROACH or terrible parasite of the IT world,
distributing malware
without any permission or affiliation from npm library creators including me.
CAUTION: This library name is 'typeorm-faker', NOT 'typeorm-faker-js'
I must to stuff this This creature... this abomination... with profile link and capture.
- https://www.npmjs.com/~vdtn359
- !alt text


Generate fake mocks, stubs using your entity settings.
you should install faker.js and typerom
``
npm install --save-dev sinon
npm install --save-dev @faker-js/faker
npm install --save-dev typeorm-faker
`
or
`
yarn add typeorm
yarn add --dev sinon
yarn add --dev @faker-js/faker
yarn add --dev typeorm-faker
`
`typescript
// import typeorm faker on your test file.
import typeormFaker from 'typeorm-faker';
// here is entity
@Entity('post')
export class Post {
@PrimaryGeneratedColumn()
id!: number;
@Column()
title!: string;
@Column()
text?: string;
@Column({
nullable: false,
default: 0
})
likesCount!: number;
}
// generate stubs
const postStubs = typeormFaker.stub(Post);
/**
* You can specify values for fake entity
*
* Post [
{ id: 1, title: 'this is test title', text: 'eW2p3tjA', likesCount: 0 },
* { id: 1, title: 'this is test title', text: 'abcdef', likesCount: 0 }
* ]
*/
const count = 5;
const postStubs = typeormFaker.stub(Post, count, {
id: 1,
title: 'this is test title'
});
// or one
/**
Post { id: 13326, title: 'pDYFMFO,ZX', text: 'eW2p3tjA', likesCount: 0 }
*/
const postStub = typeormFaker.stubOne(Post);
/**
* Post { id: 1, ... }
*/
const postStub = typeormFaker.stubOne(Post, {
id: 1
});
/**
* you can generate also typeorm raw one for type inference
*
* Post {
* post_id: 13326,
* post_title: 'pDYFMFO,ZX',
post_text: 'eW2p3tjA',
* post_likesCount: 0
* }
*/
const rawPostStub = typeormFaker.stubRawOne
const rawPostStub = typeormFaker.stubRawOne
// post_id
// post_title ...
});
const count = 2;
const rawPostStubArray = typeormFaker.stubRaw
post_id: 12345
});
`

---
setting up to tsconfig.json
`json`set typeRoots on tsconfig.json in compilerOptions
{
// ...,
"typeRoots": [
"node_modules/typeorm-faker/@types"
],
"types": [
"sample"
]
}
Set register file as require settings
`yml``like .mocharc.yml
- typeorm-faker/registers
For type inference of TypeORM raw entity, you must specify class variable and that name, additional fields in test that why
Typescript does not yet support the way that getting class name from generics.
Here is Sample Project.