thoughtbot / fishery

A library for setting up JavaScript objects as test data
MIT License
877 stars 36 forks source link

Share sequence between extended factories #51

Closed stevehanson closed 3 years ago

stevehanson commented 3 years ago

This fixes an issue where using factory extension methods causes the sequences to diverge and be independent. Since the extension methods are the core building blocks for "traits", which are intended to provide syntax sugar for building objects, this behavior is unexpected.

An example of the problem:

class UserFactory extends Factory<User> {
  admin() {
    return this.params({ admin: true });
  }
}
const userFactory = UserFactory.define({ sequence }) => {
  return {
    id: sequence,
    admin: false,
  };
});

userFactory.build().id // 1
userFactory.admin().build().id // 2 -- correct, picks up at same spot as userFactory
userFactory.build().id // 2 -- incorrect, since `admin()` generated a new factory that does not share sequence

Closes #49

andremalan commented 3 years ago

@stevehanson would it be possible to release this fix as a point release? I'm running into the same issues.