ngneat/falso

Make falso instantiable

Opened this issue · 1 comments

Description

Picture this:

function createPerson() {
  seed('person-seed');
  return {
     name: randFullName(),
     company: createCompany(),
     job: randJobTitle()
  };
}

function createCompany() {
  seed('company-seed');
  return {
    name: randCompanyName(),
    city: randCity()
  };
}

In this example, randJobTitle() is called with company-seed instead of person-seed, because the createCompany() that's called before it has changed the seed globally.

Proposed solution

In such cases it would be great if we were able to instantiate falso, like so

function createPerson() {
  const falso = new Falso();
  falso.seed('person-seed');
  return {
     name: falso.randFullName(),
     company: createCompany(),
     job: falso.randJobTitle()
  };
}

function createCompany() {
  const falso = new Falso();
  falso.seed('company-seed');
  return {
    name: falso.randCompanyName(),
    city: falso.randCity()
  };
}

This would isolate and prevent unexpected side effects like in the example above, where changing the seed somewhere affects the whole application after it.

Alternatives considered

No response

Do you want to create a pull request?

No

This is important for test runners that don't isolate the runner processes (ie. mocha, Node built-in test runner, ...). The implementation could as well be done creating an object factory, that would avoid prototype, while keeping TS interface guarantees.