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 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-byob

v1.0.3

Published

beautiful object builders for typescript

Downloads

4,415

Readme

Build Your Own Builders

byob is a Typescript micro library for creating fluent object builders for your test data.

Table of Contents

Example

// some.spec.ts
test('product list returns products according to filter', () => {
  const p1 = aProduct({name: "Banana"});
  const p2 = aProduct({name: "Bandana"});
  const p3 = aProduct({name: "Apple"});
  const catalog = new InMemoryProductCatalog([p1, p2, p3]);

  const app = render(<Gallery catalog={catalog}/>);
  fireEvent.change(app.getByTestId('name-filter'), {target: {value: 'Ban'}};

  expect(app.queryByText(p1.name)).toBeTruthy();
  expect(app.queryByText(p2.name)).toBeTruthy();
  expect(app.queryByText(p3.name)).toBeFalsy();
})

// testkit.ts
export const aProduct = builderFor<Product>({
  id: "0", name: "", price: 0, currency: "usd", dateCreated: new Date()
});

export class InMemoryProductCatalog implements ProductCatalog {
  // ...
}

// shop.ts
type Product {
  id: string;
  name: string;
  dateCreated: Date;
  price: number;
  currency: "usd" | "eur" | "ils";
}

interface ProductCatalog {
  findAll(): Promise<Product[]>
}

export const Gallery: React.FC<{catalog: ProductCatalog}> = ({catalog}) => {
  //...
}

Motivation

Tests that depend on static fixtures are harder to read, understand and maintain. This blog post started to develop the idea, and this library provides a simple, DRY way to create builders.

Installation

npm install -D ts-byob

or

yarn add -D ts-byob

Usage

Given a type Product:

type Product = {
  id: string,
  name: String,
  price: number, 
  currency: "usd" | "eur" | "ils",
  photos: Photo[],
}

Scalar initializer

If you pass an object to builderFor<T>, the object's properties will be used as defaults for all objects generated by the resulting builder:

const aProduct = builderFor<Product>({id: "0", name: "foo", price: 0, currency: "usd", photos: []});

const product1 = aProduct(); 
// {id: "0", name: "foo", price: 0, currency: "usd", photos: []}

const product2 = aProduct({name: bar});
 // {id: "0", name: "bar", price: 0, currency: "usd", photos: []}

Function initializer

If we want to create fresh values for each new object (for instance, current Date, random ID), we can provide a function initializer:

const aProductWithRandomId = builderFor<Product>(() => ({id: nanoid(), name: "foo", price: 0, currency: "usd", photos: []}));

const product1 = aProductWithRandomId(); 
// {id: <a random nanoid>, name: "foo", price: 0, currency: "usd", photos: []}

const product2 = aProductWithRandomId();
// {id: <another random nanoid>, name: "bar", price: 0, currency: "usd", photos: []}

Sequences

The function initializer can use a per-builder context to generate running sequences, useful for numeric IDs.

const aProduct = builderFor<Product>(({next}) => 
  ({id: next("id"), name: "foo", price: 0, currency: "usd", photos: []}})
);

const anOrder = builderFor<Order>(({next}) => 
  ({id: next("id"), items: []}})
);

const product1 = aProduct(); 
// {id: 1, name: "foo", price: 0, currency: "usd", photos: []}

const product2 = aProduct();
// {id: 2, name: "foo", price: 0, currency: "usd", photos: []}

const order = anOrder({products: [product1, product2]});
// {id: 1, products: [{...}, {...}]}

Contributing

We actively welcome pull requests and proposed changes to the code base. Please follow these steps when contributing.

  1. Please fork and branch from main.
  2. Comment your code extensively, and update the README when expected.
  3. Add unit tests where applicable.
  4. All existing testing suites must pass and no linter errors should occur.