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

simpl-schema-mockdoc

v1.0.5

Published

Allow to easily create mock document for jest from simpl-schema

Downloads

61

Readme

simpl-schema-mockdoc

Easily create mock document for jest from simpl-schema

Installation

npm install --save-dev simpl-schema-mockdoc

API

getMockDoc(schema, [[prefix], addId])

  • schema: an instance of SimpleSchema
  • prefix (optional): a string to be used as prefix (default 'mock')
  • addId (optional): if true, the object will be returned with a Mongo-like _id field

Given a schema, getMockDoc will generate an object that matches the schema structure, using randomly generated fake data. These values are generated using faker.js and use a seed to ensure reproducibility. The prefix is used to generate the seed and to get the mock value. For example, a String field named foo would generate the value mockFoo by default. Using getMockDoc(schema, 'myPrefix'), you would receive myPrefixFoo.

getMockDoc handles the following options in your field definition (in this order):

  • mockValue: specify the value you want to avoid random generation
  • defaultValue
  • autoValue: will try to use the autoValue function, falling back to the random generation if autoValue fails
  • allowedValues: will take a random argument in the array

For Number fields, the min and max values will also be looked up before generating a random number.

For String fields, the regEx value will be used if it matches one of the SimpleSchema.RegEx.*

Finally, if your schema contains a field defined by another schema, the function will crawl those as well to generate the document.

NB: The function will only run with NODE_ENV === 'test'

Example
import SimpleSchema from 'simpl-schema';
import { getMockDoc } from 'simpl-schema-mockdoc';

const BasicSchema = new SimpleSchema({
  arrayField: {
    type: Array,
  },
  booleanField: {
    type: Boolean,
  },
  dateField: {
    type: Date,
  },
  numberField: {
    type: Number,
  },
  objectField: {
    type: Object,
  },
  stringField: {
    type: String,
  },
});

const BasicSchemaObject = getMockDoc(BasicSchema);
/*
returns: {
  arrayField: [],
  booleanField: true,
  dateField: new Date(67032000),
  numberField: 41349,
  objectField: {},
  stringField: 'mockStringField',
};
*/

const BasicSchemaObjectPrefix = getMockDoc(BasicSchema, 'prefix');
/*
returns: {
  arrayField: [],
  booleanField: false,
  dateField: new Date(13994000),
  numberField: 65696,
  objectField: {},
  stringField: 'prefixStringField',
};
*/

const BasicSchemaObjectId = getMockDoc(BasicSchema, 'mock', true);
/*
returns: {
  _id: 'a1b2c3d4e5f6g7h8i',
  arrayField: [],
  booleanField: false,
  dateField: new Date(13994000),
  numberField: 65696,
  objectField: {},
  stringField: 'prefixStringField',
};
*/

const NestedSchema = new SimpleSchema({
  objectField: {
    type: Object,
  },
  'objectField.bar': {
    type: BasicSchema,
  },
});

const NestedSchemaObject = getMockDoc(NestedSchema);
/*
returns: {
  objectField: {
    bar: {
      arrayField: [],
      booleanField: true,
      dateField: new Date(67032000),
      numberField: 41349,
      objectField: {},
      stringField: 'mockStringField',
    },
  },
};
*/

clearMockValues(schema)

Used to remove any mockValue instance existing in your schema. Will only run with NODE_ENV !== 'test'

import SimpleSchema from 'simpl-schema';
import { clearMockValues } from 'simpl-schema-mockdoc';

const BasicSchemaWithMock = new SimpleSchema({
  arrayField: {
    type: Array,
    mockValue: ['foo'],
  },
  booleanField: {
    type: Boolean,
    mockValue: true,
  },
  dateField: {
    type: Date,
    mockValue: new Date(86400000),
  },
  numberField: {
    type: Number,
    mockValue: 42,
  },
  objectField: {
    type: Object,
    mockValue: { bar: 'baz' },
  },
  stringField: {
    type: String,
    mockValye: 'myValue',
  },
});

const BasicSchema = clearMockValues(BasicSchemaWithMock);
// BasicSchema is now the one defined above