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

unit-kuznetsov

v1.1.1

Published

Unit Test Library

Downloads

5

Readme

Unit Test Library

Library for simple creating unit tests.

Content.

##GitHub Progect on GitHub.

Install.

npm install unit-kuznetsov --save-dev

How to use.

The library exports two class Unit and Test.

Class Unit.

Constructor.

Class Unit creates an object which perform a set of unit tests for a function. It's constructor gets three arguments:

  • func: function - Function you want to test
  • description: string - Text which describes this test set.
  • context: object - Object of testing function`s context. This argument is optional and I srongly recomed to not use it because unit tests are good for pure functions that depend on their arguments only and _do not depend on their environment.
const unit = new Unit(x => x, 'Simple test of the simple function');

Methods.

Class Unit has some methods:

  • addTest gets one argument the object of class Test which describes single test. addTest adds test description into test sequence.
  • commitTests commits current test sequence.
  • getResult returns object with result of the last commit of the test sequence. If test sequence has not commited it calls addTest method first.
  • drawresult gets two optional arguments.
    • First boolean argumet failsOnly and displays the result of the last commit of the test sequence. The result drawResult gets by calling getResult first. By default failsOnly = false, if it is true drawResult would show only fallen tests.
    • Secondobject argument styles has options which redefine default styles. Nextoptions are available:
      • main - main style.
      • description - test description's style.
      • empty - style of non test message.
      • date - style of dates of start and finish test.
      • duration - style of test's duration.
      • success - style of success message.
      • fail - style of fail message.
      • exeption - style of exeption's message.
      • sum - style of value of test's quantity.
      • testIndex - style of current test's index.
      • resultBase - base style of current test's result.
      • methodName - style of testing method's name.
      • args - style of argument's list.
      • expectation - style of expetcation value.
      • resultValue - style of function's result n current test.

Note: all methods exept getResult returns current context of Unit, so you can use these methods in chains.
Note: if you are adding new test into a sequence after the sequence has been commited to get or display the test's result you need call commitTests method before getResult and drawResult methods.

unit.addTest(new Test({
	arg: 5,
	expectation: 5,
	method: 'isEqual'
})).drawResult().addTest(new Test({
	arg: 8,
	expectation: 8,
	method: 'isEqual'
})).commitTests().drawResult(true);

Class Test.

Class Test is a simple class which describes single test algorithm. Test was added to simplify validating description of test algorithm. Test does not have any own methods exept the constructor. Constructor requires the only object argument which describes test algorithm, here the list of it's fields:

  • method: string | function - Name of the verification method algorithm or method's function will use to check function. This field is needed. Note: in case of method is function, it has to accept two arguments: first - value from tested function, second - expectedvalue and return boolean: true or false. The function should be named.
  • arg: * - Argument for testing function.
  • args: Array - Array of arguments for testing function. Note: if field args exists arg will be ignored.
  • expectation: * - The value we are expecting as a result of testing function.
const test = new Test({
	arg: 8,
	expectation: 8,
	method: 'isEqual'
});

Verification Methods.

Simple data types.

  • isEqual. Usable with simple data types. Checks if returned value is equal to expeted value. Method uses a strict comparison.
  • isEqualnonStrict. Usable with simple data types. Checks if returned value is equal to expeted value. Method uses a nonstrict comparison.

Check types.

  • exist. Checks if returned value is not undefined.
  • notExist. Checks if returned value is undefined.
  • isNull. Checks if returned value is null.
  • isNotNull. Checks if returned value is not null.
  • isNill. Checks if returned value is null or undefined.
  • isNotNill. Checks if returned value is not null or undefined.
  • isNaN. Checks if returned value is NaN.
  • isNotNaN. Checks if returned value is not NaN.
  • isTrue. Checks if returned value can be interpreted as true.
  • isFalse. Checks if returned value can be interpreted as false.
  • isTypeOf. Checks if returned values type is equal to expectation`.
  • isInstanceOf. Checks if returned value is instance of expectation.

Complex data types.

  • isSameStructure. Checks if returned value has same structure with expectation.
  • isSameNotOrderedStructure. Checks if returned value has same structure with expectation without dependency on order.
  • isLikeStructure. Checks if returned value data equals expectation or if they are objects it has properties from expectation and they are like

Example.

import {Unit, Test}	from 'unit-kuznetsov';

const testUnit_1 = new Unit (x => x, 'Check "isSameStructure"');
const testUnit_2 = new Unit (x => x, 'Check "isSameNotOrderedStructure"');
const testUnit_3 = new Unit (x => x, 'Check "isLikeStructure"');

const test_obj_1 = {
     item1: 2,
     item2: '3',
     item3: [],
     test: 'test',
     any: false,
     obj: {
          test: 123,
          arr: [12, 3, 'test', {}, [1, 2, 7, {d: 2}], ''],
          obj: {
               'null': null,
               nan: NaN,
               u: undefined,
               'true': true
          }
     }
};

const test_obj_2 = {
     item_1: 2,
     item2: '3',
     item3: [],
     test: 'test',
     any: false,
     obj: {
          test: 123,
          arr: [12, 3, 'test', {}, [1, 2, 7, {d: 2}], ''],
          obj: {
               'null': null,
               nan: NaN,
               u: undefined,
               'true': true
          }
     }
};

const test_obj_3 = {
     item2: '3',
     item1: 2,
     item3: [],
     test: 'test',
     any: false,
     obj: {
          test: 123,
          arr: [12, 3, 'test', {}, [1, 2, 7, {d: 2}], ''],
          obj: {
               'null': null,
               nan: NaN,
               u: undefined,
               'true': true
          }
     }
};


testUnit_1.addTest(new Test({arg: 5, method: 'isSameStructure', expectation: 5}))
          .addTest(new Test({arg: 5, method: 'isSameStructure', expectation: '5'}))
          .addTest(new Test({arg: '5', method: 'isSameStructure', expectation: 5}))
          .addTest(new Test({arg: '5', method: 'isSameStructure', expectation: '5'}))
          .addTest(new Test({arg: 6, method: 'isSameStructure', expectation: 5}))
          .addTest(new Test({arg: 'string', method: 'isSameStructure', expectation: '5'}))
          .addTest(new Test({arg: undefined, method: 'isSameStructure', expectation: undefined}))
          .addTest(new Test({arg: null, method: 'isSameStructure', expectation: null}))
          .addTest(new Test({arg: NaN, method: 'isSameStructure', expectation: NaN}))
          .addTest(new Test({arg: null, method: 'isSameStructure', expectation: {}}))
          .addTest(new Test({arg: null, method: 'isSameStructure', expectation: undefined}))
          .addTest(new Test({arg: {}, method: 'isSameStructure', expectation: {}}))
          .addTest(new Test({arg: [], method: 'isSameStructure', expectation: {}}))
          .addTest(new Test({arg: [], method: 'isSameStructure', expectation: []}))
          .addTest(new Test({arg: new Date(), method: 'isSameStructure', expectation: {}}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isSameStructure', expectation: [0, 1, 2, 3]}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isSameStructure', expectation: [0, 1, 2, 3, 4]}))
          .addTest(new Test({arg: [0, 1, 2], method: 'isSameStructure', expectation: [0, 1, 2, 3]}))
          .addTest(new Test({arg: [0, 2, 1, 3], method: 'isSameStructure', expectation: [0, 1, 2, 3]}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isSameStructure', expectation: {'0': 0, '1': 1, '2': 2, '3': 3}}))
          .addTest(new Test({arg: {'0': 0, '1': 1, '2': 2, '3': 3}, method: 'isSameStructure', expectation: {'0': 0, '1': 1, '2': 2, '3': 3}}))
          .addTest(new Test({arg: {...test_obj_1}, method: 'isSameStructure', expectation: {...test_obj_1}}))
          .addTest(new Test({arg: {...test_obj_1}, method: 'isSameStructure', expectation: {...test_obj_2}}))
          .addTest(new Test({arg: {...test_obj_2}, method: 'isSameStructure', expectation: test_obj_2}))
          .addTest(new Test({arg: {...test_obj_1}, method: 'isSameStructure', expectation: {...test_obj_3}}))
          .drawResult();

testUnit_2.addTest(new Test({arg: 5, method: 'isSameNotOrderedStructure', expectation: 5}))
          .addTest(new Test({arg: 5, method: 'isSameNotOrderedStructure', expectation: '5'}))
          .addTest(new Test({arg: '5', method: 'isSameNotOrderedStructure', expectation: 5}))
          .addTest(new Test({arg: '5', method: 'isSameNotOrderedStructure', expectation: '5'}))
          .addTest(new Test({arg: 6, method: 'isSameNotOrderedStructure', expectation: 5}))
          .addTest(new Test({arg: 'string', method: 'isSameNotOrderedStructure', expectation: '5'}))
          .addTest(new Test({arg: undefined, method: 'isSameNotOrderedStructure', expectation: undefined}))
          .addTest(new Test({arg: null, method: 'isSameNotOrderedStructure', expectation: null}))
          .addTest(new Test({arg: NaN, method: 'isSameNotOrderedStructure', expectation: NaN}))
          .addTest(new Test({arg: null, method: 'isSameNotOrderedStructure', expectation: {}}))
          .addTest(new Test({arg: null, method: 'isSameNotOrderedStructure', expectation: undefined}))
          .addTest(new Test({arg: {}, method: 'isSameNotOrderedStructure', expectation: {}}))
          .addTest(new Test({arg: [], method: 'isSameNotOrderedStructure', expectation: {}}))
          .addTest(new Test({arg: [], method: 'isSameNotOrderedStructure', expectation: []}))
          .addTest(new Test({arg: new Date(), method: 'isSameNotOrderedStructure', expectation: {}}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isSameNotOrderedStructure', expectation: [0, 1, 2, 3]}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isSameNotOrderedStructure', expectation: [0, 1, 2, 3, 4]}))
          .addTest(new Test({arg: [0, 1, 2], method: 'isSameNotOrderedStructure', expectation: [0, 1, 2, 3]}))
          .addTest(new Test({arg: [0, 2, 1, 3], method: 'isSameNotOrderedStructure', expectation: [0, 1, 2, 3]}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isSameNotOrderedStructure', expectation: {'0': 0, '1': 1, '2': 2, '3': 3}}))
          .addTest(new Test({arg: {'0': 0, '1': 1, '2': 2, '3': 3}, method: 'isSameNotOrderedStructure', expectation: {'0': 0, '1': 1, '2': 2, '3': 3}}))
          .addTest(new Test({arg: {...test_obj_1}, method: 'isSameNotOrderedStructure', expectation: {...test_obj_1}}))
          .addTest(new Test({arg: {...test_obj_1}, method: 'isSameNotOrderedStructure', expectation: {...test_obj_2}}))
          .addTest(new Test({arg: {...test_obj_2}, method: 'isSameNotOrderedStructure', expectation: test_obj_2}))
          .addTest(new Test({arg: {...test_obj_1}, method: 'isSameNotOrderedStructure', expectation: {...test_obj_3}}))
          .drawResult();

testUnit_3.addTest(new Test({arg: 5, method: 'isLikeStructure', expectation: 5}))
          .addTest(new Test({arg: 5, method: 'isLikeStructure', expectation: '5'}))
          .addTest(new Test({arg: '5', method: 'isLikeStructure', expectation: 5}))
          .addTest(new Test({arg: '5', method: 'isLikeStructure', expectation: '5'}))
          .addTest(new Test({arg: 6, method: 'isLikeStructure', expectation: 5}))
          .addTest(new Test({arg: 'string', method: 'isLikeStructure', expectation: '5'}))
          .addTest(new Test({arg: undefined, method: 'isLikeStructure', expectation: undefined}))
          .addTest(new Test({arg: null, method: 'isLikeStructure', expectation: null}))
          .addTest(new Test({arg: NaN, method: 'isLikeStructure', expectation: NaN}))
          .addTest(new Test({arg: null, method: 'isLikeStructure', expectation: {}}))
          .addTest(new Test({arg: null, method: 'isLikeStructure', expectation: undefined}))
          .addTest(new Test({arg: {}, method: 'isLikeStructure', expectation: {}}))
          .addTest(new Test({arg: [], method: 'isLikeStructure', expectation: {}}))
          .addTest(new Test({arg: [], method: 'isLikeStructure', expectation: []}))
          .addTest(new Test({arg: new Date(), method: 'isLikeStructure', expectation: {}}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isLikeStructure', expectation: [0, 1, 2, 3]}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isLikeStructure', expectation: [0, 1, 2, 3, 4]}))
          .addTest(new Test({arg: [0, 1, 2], method: 'isLikeStructure', expectation: [0, 1, 2, 3]}))
          .addTest(new Test({arg: [0, 2, 1, 3], method: 'isLikeStructure', expectation: [0, 1, 2, 3]}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isLikeStructure', expectation: {'0': 0, '1': 1, '2': 2, '3': 3}}))
          .addTest(new Test({arg: {'0': 0, '1': 1, '2': 2, '3': 3}, method: 'isLikeStructure', expectation: {'0': 0, '1': 1, '2': 2, '3': 3}}))
          .addTest(new Test({arg: {...test_obj_1}, method: 'isLikeStructure', expectation: {...test_obj_1}}))
          .addTest(new Test({arg: {...test_obj_1}, method: 'isLikeStructure', expectation: {...test_obj_2}}))
          .addTest(new Test({arg: {...test_obj_2}, method: 'isLikeStructure', expectation: test_obj_2}))
          .addTest(new Test({arg: {...test_obj_1}, method: 'isLikeStructure', expectation: {...test_obj_3}}))
          .addTest(new Test({arg: [0, 1, 2, 3], method: 'isLikeStructure', expectation: [0, 1, 2]}))
          .addTest(new Test({arg: {'0': 0, '1': 1, '2': 2, '3': 3}, method: 'isLikeStructure', expectation: {'0': 0, '1': 1, '3': 3}}))
          .addTest(new Test({arg: {'0': 0, '1': 1, '3': 3}, method: 'isLikeStructure', expectation: {'0': 0, '1': 1, '2': 2, '3': 3}}))
          .drawResult();