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

@makerx/ts-dossier

v3.0.0

Published

A support library to facilitate the easy creation of builders for use with an Object-Mother test pattern in TypeScript

Downloads

1,008

Readme

TypeScript Dossier (ts-dossier)

A support library to facilitate the easy creation of test data builders for use with an Object-Mother pattern in TypeScript

npm package Build Status Downloads Issues Semantic Release

Install

npm install @makerx/ts-dossier --save-dev

Usage

The first step is to define a model

type Colour = 'Blue' | 'Red' | 'Yellow' | 'Green'

export type Shape = {
  name: string
  sides: number
  sideLengths: number[]
  colour: Colour
}

Then define a Builder for that model

import { randomElement, randomNumberBetween, randomString } from '@makerx/ts-dossier'
import { DataBuilder, dossierProxy } from '@makerx/ts-dossier'
import { Shape } from './shape'

function generateSideLengths(sides: number) {
  return [...Array(sides).keys()].map((_) => randomNumberBetween(1, 999))
}

class ShapeBuilder extends DataBuilder<Shape> {
  constructor() {
    const sides = randomNumberBetween(1, 4)
    super({
      name: randomString(10, 20),
      sides,
      sideLengths: generateSideLengths(sides),
      colour: randomElement(['Blue', 'Red', 'Yellow', 'Green']),
    })
  }

  public withSides(sides: number) {
    this.with('sides', sides)
    if (this.thing.sideLengths.length != sides) {
      this.with('sideLengths', generateSideLengths(sides))
    }
    return this
  }

  public asSquare(length: number) {
    this.thing = {
      ...this.thing,
      name: 'Square',
      sides: 4,
      sideLengths: [length, length, length, length],
    }
    return this
  }

  public asIsoscelesTriangle(length: number, perimeter: number) {
    this.thing = {
      ...this.thing,
      name: 'Isosceles triangle',
      sides: 3,
      sideLengths: [length, length, perimeter - length * 2],
    }
    return this
  }
}

export const shapeBuilder = dossierProxy<ShapeBuilder, Shape>(ShapeBuilder)

With the builder defined, and using the dossier proxy, you now have access to the builder methods supplied by the builder itself and the ones defined for you by the Dossier proxy.

Intellisense Example

Then define a mother to build known models for testing

import { shapeBuilder } from './shape-builder'

export const shapeMother = {
  blueSquare: () => {
    return shapeBuilder().asSquare(20).withColour('Blue')
  },
  greenTriangle: () => {
    return shapeBuilder().withName('Triangle').withSides(3).withColour('Green')
  },
  redIsoscelesTriangle: () => {
    return shapeBuilder().asIsoscelesTriangle(20, 45).withColour('Red')
  },
}

And write some tests

import { describe, expect, it } from '@jest/globals'
import { shapeMother } from './shape-mother'

describe('The square', () => {
  it('has four sides', () => {
    const shape = shapeMother.blueSquare().build()
    expect(shape.sides).toBe(4)
  })
  it('has four sides of equal length', () => {
    const shape = shapeMother.blueSquare().build()
    expect(shape.sideLengths).toEqual(expect.arrayContaining([...Array(4)].map((_) => shape.sideLengths[0])))
  })
  it('is named correctly', () => {
    const shape = shapeMother.blueSquare().build()
    expect(shape.name).toBe('Square')
  })
  it('is coloured blue', () => {
    const shape = shapeMother.blueSquare().build()
    expect(shape.colour).toBe('Blue')
  })
})
describe('The isosceles triangle', () => {
  it('has three sides', () => {
    const shape = shapeMother.redIsoscelesTriangle().build()
    expect(shape.sides).toBe(3)
  })
  it('has two sides of equal length', () => {
    const shape = shapeMother.redIsoscelesTriangle().build()
    expect(shape.sideLengths.reduce<number[]>((a, c) => (a.includes(c) ? a : [...a, c]), [])).toHaveLength(2)
  })
  it('is named correctly', () => {
    const shape = shapeMother.redIsoscelesTriangle().build()
    expect(shape.name).toBe('Isosceles triangle')
  })
  it('is coloured red', () => {
    const shape = shapeMother.redIsoscelesTriangle().build()
    expect(shape.colour).toBe('Red')
  })
})

Try it out on StackBlitz

Random Data Builders

Dossier comes with a variety of random data builders - View detailed function descriptions includes arguments in the code docs.

| Name | Function | Other | |---------------------------|---------------------|------------------------------------------------------------------------------| | Number | randomNumber | | | Number between | randomNumberBetween | | | Float between | randomFloatBetween | | | String | randomString | | | ID | randomId | | | Date | randomDate | | | Date between | randomDateBetween | | | Boolean | randomBoolean | | | Incremented number | incrementedNumber | Returns a unique incremented number. Call resetIncrementedNumbers to reset | | Element from a collection | randomElement | | | Name of a thing | randomThingName | | | Name of a person | randomPersonName | | | Email | randomEmail | | | Phone number | randomPhoneNumber | | | URL | randomUrl | |