A JavaScript library that takes the pain out of mocking deep objects.
Installation
Install the package with npm:
npm install mocktail-js
Include it in your project:
import mocktail from "mocktail-js";
Documentation
MocktailJS builds a JavaScript object off of a string pattern. The deepest keys may be assigned values, left to right, based on the passed pattern.
Mock deep object
Use period character to indicate parent-child relation.
mocktail("foo.bar.baz", 123);
/*
* Returned object:
*
* {
* foo: {
* bar: {
* baz: 123
* }
* }
* }
*/
Mock deep and wide object
Use comma character to indicate sibling relations.
function getStuff() { return "stuff"; }
mocktail("foo.bar,boo.baz", null, getStuff);
/*
* Returned object:
*
* {
* foo: {
* bar: null
* },
* boo: {
* baz: getStuff
* }
* }
*/
Mock deep and wide object with forks
Use colon character to indicate forking and semicolon to terminate it.
mocktail("foo.bar,boo:baz.one,ban.two.three;", 123, null, "awesome");
/*
* Returned object:
*
* {
* foo: {
* bar: 123
* },
* boo: {
* baz: {
* one: null
* },
* ban: {
* two: {
* three: "awesome"
* }
* }
* }
* }
*/