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

chaos_engine_destructive_tests

v1.0.0

Published

Run destructive tests on functions

Downloads

2

Readme

chaos_engine_destructive_tests

Chaos engine runs destructive tests on user defined functions by passing edge case inputs and returning an array of objects with each input detailing the function output(s) and error(s) thrown if any.

Installation

npm install chaos_engine_destructive_tests

Usage

Input: Java script object

{
    functionToTest (required, type: function): This is the function you wish to test,

    numOfInputs (required, type: Number): The number of inputs you want to pass to the function (cannot be less than the function's arity),

    customInputs (optional, type: Array of array(s)): Any other inputs you would like to experiment with apart from the native destructive inputs passed by the package,

    returnsPromise (optional, type: Boolean): This flag should be set to true if your specified function returns a promise
}

Output: Array of objects

chaos_engine returns an array of objects with each object corresponding to each set of inputs with properties describing the input type, input value, value returned, error name and error message. A boolean error property shows whether or not the set of inputs threw an error e.g. The sample output below shows a set of inputs that triggered an error as well as another set that returned an output.

[
    {
        error_name: 'TypeError',
        error_message: 'first.split is not a function',
        error: true,
        input_type: 'Number',
        input_value: '(-1,-1,-1)'
    },
    {
        error: false,
        input_type: 'String',
        input_value: '(@*#$%!-+(),@*#$%!-+(),@*#$%!-+())',
        function_output: [
        '@', '*', '#', '$',
        '%', '!', '-', '+',
        '(', ')'
        ]
    }
]

Examples

1. Pass function and Number of inputs

const chaos = require('chaos-engine-destructive-tests')

function trialFunction(firstInput, secondInput) {
    firstInput.length * secondInput.length
}

console.log(chaos({functionToTest: trialFunction, numOfInputs: 2}))

output: [
  {
    error: false,
    input_type: 'Number',
    input_value: '(0,0)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(100000000000000000000,100000000000000000000)',  
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(-100000000000000000000,-100000000000000000000)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(0,0)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(0,0)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(5,5)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(-5,-5)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(1,1)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(-1,-1)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(@*#$%!-+(),@*#$%!-+())',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(dimensions,dimensions)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(mdkwieew,mdkwieew)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(2933874,2933874)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(dkms3223@#dkdm,dkms3223@#dkdm)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(,)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(dude,guy,13832,dk82dk,dude,guy,13832,dk82dk)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(some,random,objects,some,random,objects)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(828,392,18332037,235,828,392,18332037,235)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Objects',
    input_value: '([object Object],[object Object])',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Objects',
    input_value: '([object Object],[object Object])',
    function_output: undefined
  }
] 

2. Specify additional inputs

Say you would like to pass two sets of custom defined inputs: ('dkdm292993', Infinity), (5, 12) to the function, you should pass them as an array of arrays to the customInputs property as shown below

const chaos = require('chaos-engine-destructive-tests')

function trialFunction(firstInput, secondInput) {
    firstInput.length * secondInput.length
}

console.log(chaos({functionToTest: trialFunction, numOfInputs: 2}, customInputs: [['dkdm292993', Infinity], [5, 12]]))

output: [
  {
    error: false,
    input_type: 'Custom input',
    input_value: '(dkdm292993,Infinity)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Custom input',
    input_value: '(5,12)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(0,0)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(100000000000000000000,100000000000000000000)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(-100000000000000000000,-100000000000000000000)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(0,0)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(0,0)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(5,5)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(-5,-5)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(1,1)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(-1,-1)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(@*#$%!-+(),@*#$%!-+())',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(dimensions,dimensions)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(mdkwieew,mdkwieew)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(2933874,2933874)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(dkms3223@#dkdm,dkms3223@#dkdm)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(,)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(dude,guy,13832,dk82dk,dude,guy,13832,dk82dk)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(some,random,objects,some,random,objects)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(828,392,18332037,235,828,392,18332037,235)',
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Objects',
    input_value: '([object Object],[object Object])',       
    function_output: undefined
  },
  {
    error: false,
    input_type: 'Objects',
    input_value: '([object Object],[object Object])',       
    function_output: undefined
  }
]

3. Promises and Asynchronous functions

When passing functions that return promises, always remember to set the returnsPromise property to true

const chaos = require('chaos_engine_destructive_tests')

function someAsynFunction(firstInput, secondInput) {
   return new Promise((resolve, reject){
      if (firstInput) {
         resolve(secondInput);     
      } else {
         reject(0)
      }
   });
}

console.log(chaos({functionToTest: someAsyncFunction, numOfInputs: 2, returnsPromise: true}))

output: [
  {
    error: false,
    input_type: 'Number',
    input_value: '(0,0)',
    function_output: Promise { <rejected> 0 }
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(100000000000000000000,100000000000000000000)',
    function_output: Promise { 100000000000000000000 }
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(-100000000000000000000,-100000000000000000000)',
    function_output: Promise { -100000000000000000000 }
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(0,0)',
    function_output: Promise { <rejected> 0 }
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(0,0)',
    function_output: Promise { <rejected> 0 }
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(5,5)',
    function_output: Promise { 5 }
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(-5,-5)',
    function_output: Promise { -5 }
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(1,1)',
    function_output: Promise { 1 }
  },
  {
    error: false,
    input_type: 'Number',
    input_value: '(-1,-1)',
    function_output: Promise { -1 }
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(@*#$%!-+(),@*#$%!-+())',
    function_output: Promise { '@*#$%!-+()' }
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(dimensions,dimensions)',
    function_output: Promise { 'dimensions' }
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(mdkwieew,mdkwieew)',
    function_output: Promise { 'mdkwieew' }
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(2933874,2933874)',
    function_output: Promise { '2933874' }
  },
  {
    error: false,
    input_type: 'String',
    input_value: '(dkms3223@#dkdm,dkms3223@#dkdm)',
    function_output: Promise { 'dkms3223@#dkdm' }
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(,)',
    function_output: Promise { [] }
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(dude,guy,13832,dk82dk,dude,guy,13832,dk82dk)',
    function_output: Promise { [Array] }
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(some,random,objects,some,random,objects)',
    function_output: Promise { [Array] }
  },
  {
    error: false,
    input_type: 'Array',
    input_value: '(828,392,18332037,235,828,392,18332037,235)',
    function_output: Promise { [Array] }
  },
  {
    error: false,
    input_type: 'Objects',
    input_value: '([object Object],[object Object])',
    function_output: Promise { {} }
  },
  {
    error: false,
    input_type: 'Objects',
    input_value: '([object Object],[object Object])',
    function_output: Promise { [Object] }
  }
]

Errors

Most errors are probably due to passing inputs wrongly. Here are a couple of things that could be wrong if you get an error after calling chaos_engine_destructive_tests:

  1. Passing a numOfInputs value that is less than the function's number of formal arguments e.g.
const chaos = require('chaos_engine_destructive_tests')

function trialFunction(firstInput, secondInput) {
    firstInput.length * secondInput.length
}

console.log(chaos({functionToTest: trialFunction, numOfInputs: 1}))

output: Error: Number of inputs cannot be less than number of formal function parameters
  1. Validation errors: Passing the wrong variable types will result in an error (refer to the usage section for the appropriate types for each input property) e.g.
const chaos = require('chaos_engine_destructive_tests')

function trialFunction(firstInput, secondInput) {
    firstInput.length * secondInput.length
}

console.log(chaos({functionToTest: trialFunction, numOfInputs: 'w'}))

output: ValidationError: number of inputs should be a number

In the example below, the second custom input argument is passed as a Number instead of an array

const chaos = require('chaos_engine_destructive_tests')

function trialFunction(firstInput, secondInput) {
    firstInput.length * secondInput.length
}

console.log(chaos({functionToTest: trialFunction, numOfInputs: 2, customInputs: [[3,4], 5]}))

output: Error: Custom inputs must be an array of arrays.