Giter VIP home page Giter VIP logo

unit-test-sentinel's Introduction

Unit test sentinel

Provides unique objects for unit tests in JavaScript. Inspired by the sentinel object in Python.

It's useful for creating a value that is passed into a function call, but you don't want to keep track of this value in a local variable. Instead, you can ask the sentinel to give you a unique named property that will persist through your test.

Installation

npm install --save-dev unit-test-sentinel

Usage

Import the module sentinel and then create a sentinel object for each test. This example uses Mocha and calls the sentinel object ф (which is U+0444).

import sentinel from 'unit-test-sentinel'

// We'll test this function.
const logAndReturn = (input) => {
  console.log(input);
  return input;
};

describe('logAndReturn', () => {
  let ф;

  beforeEach(() => {
    // Create a sentinel object, which can contain arbirary properties.
    ф = sentinel.create();
  })

  it('returns its input', () => {
    // Pass in a custom input, as a named property.
    const result = logAndReturn(ф.customInput);

    // Expect that the output is the exact same object that was passed in.
    expect(result).to.equal(ф.customInput);
  })
});

This example require()s the sentinel module, uses assert for testing, and calls the sentinel object s. It also uses the random method, calling its object r.

const sentinel = require('unit-test-sentinel');
const s = sentinel.create();
const r = sentinel.random();

const first = s.hello; // => ¦ф.hello¦
const second = s.hello; // => ¦ф.hello¦
assert(first === second); // ✓

const r1 = s.randomized; // e.g. => randomized-WfQ8S;0{|+)t
const r2 = s.randomized; // => randomized-WfQ8S;0{|+)t
assert(r1 === r2); // ✓

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.