npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fluentbuilder

v2.1.0

Published

A fluent way to create builders for tests

Downloads

509

Readme

FluentBuilder

npm version Build Status Coverage Status

FluentBuilder provides a simple way for creating data builders for your tests, with all the beauty of a good intellisense.

It's use Faker.Js for fake data.

Installation

Nothing new here, just npm install

npm i --save-dev fluentbuilder

Get Started

Let's define an interface (can be a class or anything like that)

interface Foo {
    id: number,
    name: string
}

Then define a shape for your builder and use the addShape method, which receives a factory function. It will use your shape to generate your test data

  import Builder from 'fluentbuilder'

  const builder = new Builder<Foo>()
  builder.addShape(() => ({ id: 1, name: 'bar' }))

  builder.generate() // { id: 1, name: 'bar' }

You can use the createBuilder function

  import { createBuilder } from 'fluentbuilder'
  const builder = createBuilder<Foo>(() => ({ id: 1, name: 'bar' }))

This example is not very exciting, but if we put some Faker.Js we can do better!

The delegate used can receive a parameter, which is an instance of FajerJs

import { createBuilder } from 'fluentbuilder'

const builder = createBuilder<Foo>(faker => ({ 
    id: faker.random.number(),
    name: faker.name.firstName()
}))

builder.generate() // { id: 37566, name: 'Marquis' }
builder.generate() // { id: 7487, name: 'Joy' }
builder.generate(2) // [ { id: 35751, name: 'Opal' }, { id: 94291, name: 'Savion' } ]

Like that, every time we call generate() we will have new data. Note the fact that if we pass a number as an argument to the generate(n) method, it will return an array of your type of the specified size.

Random data

You can generate a random size collection of data using the method generateRandom or using the exported function with the same name.

import { createBuilder } from 'fluentbuilder'

const builder = createBuilder<Foo>(faker => ({ 
    id: faker.random.number(),
    name: faker.name.firstName()
}))

builder.generateRandom(2) // generate from 1 to 2 items
builder.generateRandom(10,20) // generate from 10 to 20 items

it can be useful for nested array on types

interface Bar {
    baz: number,
    qux: string,
    foos: Foo[],
}

import { createBuilder, generateRandom } from 'fluentbuilder'

const builder = createBuilder<Bar>(faker => ({
  baz: faker.random.number(),
  qux: faker.name.firstName(),
  foos: generateRandom<Foo>(() => ({
      id: faker.random.number(),
      name: faker.name.firstName()
  }), 4)
}))

builder.generate() // { baz: 1,qux: 'some',foos: [ { id: 12, name: 'Steve' }, { id: 5, name: 'Jack' } ] }

Fluent Style

You can define rules for each of the properties in your type suing the method ruleFor(), which receives the property which will be populated, and a value function or a raw value

builder.ruleFor("id", faker => faker.random.number())

We have great intellisense/autocomplete help

With these methods it's easy to derive a class from Builder and make a domain specific builder

import Builder from 'fluentbuilder'

class FooBuilder extends Builder<Foo> {
    constructor(){
        super()

        // define basic props
        this.from(faker => ({
            id: faker.random.number(),
            name: faker.name.firstName()
        }))
    }

    withName(name: string): this {
        this.ruleFor("name", name);
        return this
    }
}

const fooBuilder = new FooBuilder()

fooBuilder.generate() // { id: 58431, name: 'Lesley' }
fooBuilder.withName("Fluffy").generate() // { id: 25927, name: 'Fluffy' }

The methods can be chained, so this is a valid approach


const fooFactory = () =>
    new Builder<Foo>()
    .ruleFor("id", f => f.random.number())
    .ruleFor("name", f => f.name.firstName())
    .generate()

Localization

You can change de locale with a function setLocale

suported locales can be checked here

import { setLocale } from 'fluentbuilder'

setLocale("pt_BR")