Skip to content

[feature proposal] Allow to specify factory as field default value of another factory #37

@lukaszfiszer

Description

@lukaszfiszer

Given that we have following hierarchy of interfaces:

interface Person {
  id: number;
  name: Name
  age: number;
}

interface Name {
  firstName: string;
  lastName: string;
  fullName: string;
}

I propose that it should be possible to define factories in the following way:

const nameFactory = Factory.Sync.makeFactory<Name>({
  firstName: "Bob",
  lastName: "Smith",
  fullName: "Robert J. Smith, Jr.",
});

const personFactory = Factory.Sync.makeFactory<Person>({
  id: 1,
  name: nameFactory,
  age: 10
});

When personFactory .build() is invoked with person data, the corresponding subset is passed to nameFactory.build():

personFactory.build({
  id: 9,
  name: {
    firstName: "John"
  }
});
// { id: 9 , name: { firstName: "John", lastName: "Smith", fullName: "Robert J. Smith, Jr." }, age: 10}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions