Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Subset of items for buildList should be build for each item #102

@vanatd-printify

Description

@vanatd-printify

Description

First of all, thank you guys for your hard work, this is really cool lib!
During the integration with the fishery, we found one behavior that seems wrong.
Imagine that we have Users with Posts and I what to generate a list of users with random posts, let's say 5 users with 5 posts.
To make it more useful I've done it like this:

type Post = {
  id: number;
}
type User = {
  id: number;
  posts: Post[];
};

const postFactory = Factory.define<Post>(({ sequence }) => ({ id: sequence }));

class UserFactory extends Factory<User> {
  withPosts(posts?: Post[]){
    return this.params({
      posts: posts || postFactory.buildList(5),
    })
  }
}


const userFactory = UserFactory.define(({ sequence }) => {
  return {
    id: sequence,
    posts: [],
  };
});

const users = userFactory.withPosts().buildList(5);

So after I created a list of users I expected to have 25 different posts, but I have 5 users with the same 5 posts.

To Reproduce

CodeSandbox with a failing test

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions