DEV Community

Robert Rees
Robert Rees

Posted on

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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay