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 🙏

© 2025 – Pkg Stats / Ryan Hefner

testr

v0.0.5

Published

A generic object testing package for Node.js

Readme

testr

A generic object testing package for Node.js

Install

npm install testr

Usage

var testr = require('testr');

var objToValidate = 'hello world';
var result = testr.validate(objToValidate, {_startsWith: 'hello'});

and the result is:

{
  actualValue: 'hello world',
  _startsWith: {
    passed: true,
    validationValue: 'hello'
  }
}

Validators

All validators begin with an underscore by default. You can override this by calling testr.configure with a configuration object argument like so:

testr.configure({validationPrefix: '$'});

validator functions are below:

exists

var result = testr.validate({name: 'testr'}, {name: {_exists: true}});

result:

{
    name: {
        actualValue: 'testr',
        _exists: { passed: true, validationValue: true }
    }
}

isType

var result = testr.validate('testr', {_isType: 'String'});

result:

{
    actualValue: 'testr',
    _isType: { passed: true, validationValue: 'String' }
}

startsWith

var result = testr.validate('testr', {_startsWith: 't'});

result:

{
    actualValue: 'testr',
    _startsWith: { passed: true, validationValue: 't' }
}

contains

var result = testr.validate('testr', {_contains: 't'});

result:

{
    actualValue: 'testr',
    _contains: { passed: true, validationValue: 't' }
}

lessThan

var result = testr.validate(47, {_lessThan: 48});

result:

{
    actualValue: 47,
    _lessThan: { passed: true, validationValue: 48 }
}

greaterThan

var result = testr.validate(47, {_greaterThan: 46});

result:

{
    actualValue: 47,
    _greaterThan: { passed: true, validationValue: 46 }
}

equals

var result = testr.validate('testr', {_equals: 'testr'});

result:

{
    actualValue: 'testr',
    _equals: { passed: true, validationValue: 'testr' }
}

matchesRegexPattern

var result = testr.validate('testr', {_matchesRegexPattern: 't.*'});

result:

{
    actualValue: 'testr',
    _matchesRegexPattern: { passed: true, validationValue: 't.*' }
}

arrayContains

var result = testr.validate([1, 2, 3], {_arrayContains: 3});

result:

{
    actualValue: [ 1, 2, 3 ],
    _arrayContains: { passed: true, validationValue: 3 }
}

You can also use an array as the expected value to validate that the array contains multiple values:

var result = testr.validate([1, 2, 3], {_arrayContains: [2, 3]});

result:

{
    actualValue: [ 1, 2, 3 ],
    _arrayContains: [
        { passed: true, validationValue: 2 },
        { passed: true, validationValue: 3 }
    ]
}

arrayContainsObjectWithProperties

The following examples reference this array:

var testArray = [
  {
    name: 'testr',
    description: 'generic object testing'
  }, 
  {
    name: 'billy-bob',
    age: 49
  }, 
  {
    name: 'the ugly one',
    ugliness: {
      isReallyIntense: true,
      level: 140
    }
  }
];
var result = testr.validate(testArray, {_arrayContainsObjectWithProperties: {name: 'testr'}});

result:

{
    actualValue: [
        { name: 'testr', description: 'generic object testing' },
        { name: 'billy-bob', age: 49 },
        {
            name: 'the ugly one',
            ugliness: { isReallyIntense: true, level: 140 }
        }
    ],
    _arrayContainsObjectWithProperties: {
        passed: true,
        validationValue: { name: 'testr' }
    }
}

You can also use an array as the expected value to validate multiple values:

var result = testr.validate(testArray, {_arrayContainsObjectWithProperties: [{name: 'testr'}, {name: 'billy-bob'}]});

result:

{
    actualValue: [
        { name: 'testr', description: 'generic object testing' },
        { name: 'billy-bob', age: 49 },
        {
            name: 'the ugly one',
            ugliness: { isReallyIntense: true, level: 140 }
        }
    ],
    _arrayContainsObjectWithProperties: [
        {
            passed: true,
            validationValue: { name: 'testr' }
        },
        {
            passed: true,
            validationValue: { name: 'billy-bob' }
        }
    ]
}

validateArrayObjectElement

var validationObj = {
  _validateArrayObjectElement: {
    _identifierProperties: {name: 'testr'},
    description: {
      _contains: 'testing'
    }
  }
}
var result = testr.validate(testArray, validationObj);

result:

{
    _validateArrayObjectElement: {
        _identifierProperties: { name: 'testr' },
        description: {
            actualValue: 'generic object testing',
            _contains: { passed: true, validationValue: 'testing' }
        }
    }
}

You can also use an array as the expected value to validate multiple values. Here you also see how nesting works:

validationObj = {
  _validateArrayObjectElement: [
    {
      _identifierProperties: {name: 'testr'},
      description: {
        _contains: 'testing'
      }
    },
    {
      _identifierProperties: {name: 'billy-bob'},
      age: {
        _lessThan: 100
      }
    },
    {
      _identifierProperties: {name: 'the ugly one'},
      ugliness: {
        isReallyIntense: {
          _equals: true
        },
        level: {
          _greaterThan: 125
        }
      }
    }
  ]
}
var result = testr.validate(testArray, validationObj);

result:

{
    _validateArrayObjectElement: [
        {
            _identifierProperties: { name: 'testr' },
            description: {
                actualValue: 'generic object testing',
                _contains: { passed: true, validationValue: 'testing' }
            }
        },
        {
            _identifierProperties: { name: 'billy-bob' },
            age: {
                actualValue: 49,
                _lessThan: { passed: true, validationValue: 100 }
            }
        },
        {
            _identifierProperties: { name: 'the ugly one' },
            ugliness: {
                isReallyIntense: {
                    actualValue: true,
                    _equals: { passed: true, validationValue: true }
                },
                level: {
                    actualValue: 140,
                    _greaterThan: { passed: true, validationValue: 125 }
                }
            }
        }
    ]
}

Custom Validators

Additionally you can provide custom validation functions by calling testr.configure with a configuration object argument like so:

testr.configure({
  customValidators: {
    isBlue: function(valueToCheck, whetherOrNotItShouldBeBlue) {
      var result = {};
      result.passed = ((valueToCheck === 'blue') === whetherOrNotItShouldBeBlue);
      result.validationValue = whetherOrNotItShouldBeBlue;
      return result;
    }
  }
});

Contributions are welcome