Make your Faker unit tests run faster with this one weird tip

rrees

Robert Rees

Posted on March 7, 2024

Make your Faker unit tests run faster with this one weird tip

FakerJS has a bug that is a classic example of over-eager initialisation.

Naively importing the library as per the Readme can lead to a massive slowdown in tests that are often meant to be some of the fastest in your suite.

In my case I was importing string functionality to tests content boundaries so even though I was actually trying to create any localised data I was initialising all of the localised content leading to my tests timing out for a simple validation.

The weird tip is simply to import the en locale (unless you specifically what a specific localised set of data)

import { faker } from '@faker-js/faker/locale/en';
Enter fullscreen mode Exit fullscreen mode

The English locale is also loaded for other languages so this genuinely seems the quickest import.

💖 💪 🙅 🚩
rrees
Robert Rees

Posted on March 7, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related