Skip to content

Commit

Permalink
feat(seeder): expose Factory.makeEntity method that does not call `…
Browse files Browse the repository at this point in the history
…em.persist()`

Related: #3932
  • Loading branch information
B4nan committed Jan 11, 2023
1 parent 69cf452 commit bb8f1b0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/seeder/src/Factory.ts
Expand Up @@ -11,11 +11,15 @@ export abstract class Factory<T extends object> {

protected abstract definition(faker: Faker): EntityData<T>;

private makeEntity(overrideParameters?: EntityData<T>): T {
/**
* Make a single entity instance, without persisting it.
* @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
*/
makeEntity(overrideParameters?: EntityData<T>): T {
const entity = this.em.create(this.model, {
...this.definition(faker),
...overrideParameters,
} as unknown as RequiredEntityData<T>);
} as unknown as RequiredEntityData<T>, { persist: false });

if (this.eachFunction) {
this.eachFunction(entity);
Expand Down
4 changes: 2 additions & 2 deletions tests/features/seeder/factory.test.ts
Expand Up @@ -82,14 +82,14 @@ describe('Factory', () => {
test('that a factory can make multiple instances of an entity without saving them in the database', async () => {
const projects = new ProjectFactory(orm.em).make(5);
expect(projects).toBeInstanceOf(Array);
expect(persistSpy).toBeCalledTimes(6);
expect(persistSpy).toBeCalledTimes(1);
expect(flushSpy).not.toBeCalled();
expect(projects.length).toBe(5);
});

test('that a factory can create multiple instances of an entity and save them in the database', async () => {
const projectSaved = await new ProjectFactory(orm.em).create(5);
expect(persistSpy).toBeCalledTimes(6);
expect(persistSpy).toBeCalledTimes(1);
expect(flushSpy).toBeCalledTimes(1);
expect(projectSaved).toBeInstanceOf(Array);
expect(projectSaved.length).toBe(5);
Expand Down

0 comments on commit bb8f1b0

Please sign in to comment.