Skip to content

Latest commit

 

History

History
41 lines (29 loc) · 1.25 KB

seeding.md

File metadata and controls

41 lines (29 loc) · 1.25 KB

Seeding

Seeding is a technique in Faker that allows you to get consistent results from your fixtures.

Seeding from Specs

Let's use the Dinosaur spec we created in the previous guide.

const Dinosaur = createSpec({
  id: faker.datatype.number(),
  name: faker.name.firstName(),
  location: faker.address.country(),
});

To seed your fixture, use the .seed() method before you use .generate(). These methods can even be chained together!

Dinosaur.seed(3).generate();
// { id: 55079, name: 'Antone', location: 'Pitcairn Islands' }

Seeding your spec will always result in the same fixture output. Just a heads up, the seed value is reset every time a Spec is generated.

Dinosaur.seed(3).generate();
// { id: 55079, name: 'Antone', location: 'Pitcairn Islands' }
Dinosaur.seed(3).generate();
// { id: 55079, name: 'Antone', location: 'Pitcairn Islands' }

// No more seeding!
Dinosaur.generate();
// { id: 83994, name: 'Eileen', location: 'Brazil' }

Congrats! You've successfully generate Spec fixtures with consistent results.

Up next

Learn how to easily generate multiple fixtures!