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

mocker-js

v1.0.0

Published

Faker.js factory that helps you generate data based on your project's models for testing.

Downloads

68

Readme

Motivation

Have you ever got your self looking at test files and wondering:

There's must be a better way to handle and reuse all those enormous mock objects

Then I got you covered, as that was the motivation behind mocker-js.

I was tired of seeing huge mock objects being created all over the place, most of them having the same structure and properties.

If we spend time thinking about structuring and reusing code, functions, components, why shouldn't we take some time to properly organize your tests as well?

Installation

Installing and using mocker-js is easy, you can use you preferable package manager, such as npm:

npm install mocker-js --save-dev

or yarn:

yarn add mocker-js -D

Basic example

To get you up and running in no time, here as some basic examples of how to use mocker-js.

If in you project you have for example, a classic User model, and you need to mock it, below you can se a few examples of how to do it.

Generating one mocked User

import { objectFactory } from 'mocker-js'; // <- Import mocker-js
import type { User } from "my/user/type/model";

/**
 * Create a user Factory with all its properties.
 * It's preferable to use faker to generate random data,
 * but you can always call internal functions, use static data,
 * define static values and so on..
 */
const UserFactory = objectFactory<User>((faker) => ({
  id: faker.datatype.uuid(),
  firstname: faker.name.firstName(),
  lastname: faker.name.lastName(),
  address: faker.address.streetAddress(),
  age: faker.datatype.number({ max: 100 }),
  phone: faker.phone.phoneNumber(),
}));

// Then, whenever you need to mock a User model again, you can just call:
const mockUser = UserFactory.create();

Generating a list of mocked User

import { UserFactory } from 'my/factory/of/user'; // <- Import you factory to reuse it

// Creates and return a list containing 5 unique users.
const mockUserList = UserFactory.createMany(5);

Generating a mock User with specific properties

Sometimes your function or component needs a subset of a object, like a typescript Partial<User>. In these cases you can also use your UserFactory as describe which properties do you want it to generate.

import { UserFactory } from 'my/factory/of/user';

// Creates and return a partial User with just firstname and age.
const model = UserFactory.createAndPick(["firstname", "age"]);

Generating a new custom mock User with a fixed value for one or more properties.

Sometimes you need a User mock with a custom value instead of a random one. Let's say you are testing a new parental control for you project, and constantly need to create users with a specific age.

import { UserFactory } from 'my/factory/of/user';

// Creates and return a new User factory having one or more custom fields.
const ChildModel = UserFactory.assign(faker => ({
  age: faker.datatype.number({ max: 10 }),
}));

const model = ChildModel.create();

Generating consistent mock Users

Sometimes you want your mocked object to be the same across multiple test runs, like for snapshopt testing.

import { UserFactory } from 'my/factory/of/user';

/**
 * Creates and return a new mocked user for based on the seed.
 * Every user created for the same seed will be equal.
 */
const model = UserFactory.seed(666).create();