Skip to content

Test Randomizing helps you to build randomized objects or class instances for your tests.

License

Notifications You must be signed in to change notification settings

tiloio/test-randomizing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

test-randomizing

Helps you to build randomized objects for your tests.

GitHub release GitHub stars vr scripts deno docs MIT License 0 dependencies

works in with esm, cjs... works in with esm, cjs... works with Node.js works with Deno

published on nest.land published on deno.land published on npm

🏗 Install

Deno (nest.land and deno.land)

import {
  freezeMerge,
  merge,
  RandomFn,
  freezeMergeFactory,
} from "https://x.nest.land/[email protected]/mod.ts";
// or
import {
  freezeMerge,
  merge,
  RandomFn,
  freezeMergeFactory,
} from "https://deno.land/x/[email protected]/mod.ts";

Node.js (npm.js)

npm install --save-dev test-randomizing
# or
yarn add --dev test-randomizing

🤷🏽‍♂️ How to use

You can use test randomizing in JavaScript and TypeScript projects.

We recommend using a library (like faker-js/faker or deno faker) to create randomized objects.

merge - merges two objects into one

function merge<T>(
  sourceObject: T,
  overrideObject?: DeepPartial<T>,
  options?: MergeOptions
): T;

// merges
merge({ sky: "blue", grass: "green" }, { sky: "red", sun: "yellow" });
// results in { sky: 'red', grass: 'green', sun: 'yellow'};

// merges deeply
merge(
  { grass: "green", sky: { night: "black", day: "grey" } },
  { sky: { day: "blue" } }
);
// results in { grass: 'green', sky: { night: 'black', day: 'grey' }};

// merges with undefined
merge({ sky: undefined, grass: "green" }, { sky: "blue", sun: undefined });
// results in { sky: 'blue', grass: 'green', sun: undefined };

freezeMerge - merges two objects into one and freezes the result

No diference to merge, only the result and all nested things are freezed with Object.freeze().

function freezeMerge<T>(source: T, override?: DeepPartial<T>): Readonly<T>;

// deep freezes the result
const freezedResult = freezeMerge({ sky: "blue" });
freezedResult.sky = "grey"; // change not possible
// results in { sky: 'blue' };

freezeMergeFactory - creates a function which is used to create overriden things via freeze merge

function freezeMergeFactory<T>(
  source: T
): (overrides?: DeepPartial<T>) => Readonly<T>;

// creates a, we call it 'random', function
const randomSky = freezeMergeFactory({ night: "black", day: "grey" });
randomSky({ day: "blue" });
// results in { night: 'black', day: 'blue };

Deno example - ./examples/deno:

import { freezeMergeFactory } from "https://x.nest.land/[email protected]/mod.ts";
import { faker } from "https://deno.land/x/[email protected]/mod.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

// Implementation
const generateEmail = (person: Person) => {
  if (!person.companyName || (!person.firstName && !person.lastName)) {
    return undefined;
  }

  return `${person.firstName}.${person.lastName}@${person.companyName}.com`;
};

// Types
type Person = {
  firstName: string;
  lastName: string;
  companyName: string;
};

// Test code
const randomPerson = freezeMergeFactory<Person>({
  firstName: faker.name.firstName(),
  lastName: faker.name.lastName(),
  companyName: faker.company.companyName(),
});

Deno.test("consist of [email protected]", () => {
  const person = randomPerson({
    firstName: "steve",
    lastName: "jobs",
    companyName: "apple",
  });

  const email = generateEmail(person);

  assertEquals(email, "[email protected]");
});

Deno.test("returns undefined if firstName and lastName is empty", () => {
  const person = randomPerson({ firstName: "", lastName: "" });

  const email = generateEmail(person);

  assertEquals(email, undefined);
});

Deno.test("returns undefined if companyName is empty", () => {
  const person = randomPerson({ companyName: "" });

  const email = generateEmail(person);

  assertEquals(email, undefined);
});

Typescript Node.js example - ./examples/nodejs-ts:

import { DeepPartial, freezeMerge, RandomFn } from "test-randomizing";
import { company, name } from "faker";

// Implementation
const generateEmail = (person: Person) => {
  if (!person.companyName || (!person.firstName && !person.lastName)) {
    return undefined;
  }

  return `${person.firstName}.${person.lastName}@${person.companyName}.com`;
};

// Types
type Person = {
  firstName: string;
  lastName: string;
  companyName: string;
};

// Test code
const randomPerson: RandomFn<Person> = (override?: DeepPartial<Person>) =>
  freezeMerge(
    {
      firstName: faker.name.firstName(),
      lastName: faker.name.lastName(),
      companyName: faker.company.companyName(),
    },
    override
  );

test("consist of [email protected]", () => {
  const person = randomPerson({
    firstName: "steve",
    lastName: "jobs",
    companyName: "apple",
  });

  const email = generateEmail(person);

  expect(email).toEqual("[email protected]");
});

test("returns undefined if firstName and lastName is empty", () => {
  const person = randomPerson({ firstName: "", lastName: "" });

  const email = generateEmail(person);

  expect(email).toBeUndefined();
});

test("returns undefined if companyName is empty", () => {
  const person = randomPerson({ companyName: "" });

  const email = generateEmail(person);

  expect(email).toBeUndefined();
});

More complete examples in the ./examples directory

License

MIT License

Copyright (c) 2021 Ti/o

Development

Use vr (Velociraptor) to run all commands like vr check and vr publish.

About

Test Randomizing helps you to build randomized objects or class instances for your tests.

Resources

License

Stars

Watchers

Forks

Packages

No packages published