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

attempt-test

v0.1.2

Published

Easily running a massive amount of tests with different arguments

Downloads

3

Readme

attempt

Build Status

Attempt is a small library for easily running a massive amount of tests with different arguments.

Usually, a developer will write a few, or maybe a few dozen tests verifying some method's behavior with different input. Attempt aims to make it trivial to test huge amounts of input arguments in a legible way. This should make your test suite shorter and easier to reason about, which in turn making tests easier to write and maintain.

The idea of attempt is that you define the behavior of your program rather than passing actual values. Once you have done this attempt will try to find any edge cases by passing random values to your function. Once a test returns false or throws, it makes the test fail with a descriptive error message.

Below is an example of a failing test:

 FAIL  __tests__/foo.js
  ● foo › should do something

    Test returned false
    Test failed after 363 iterations when given input
    3
    53

Installation

To install the current stable version:

npm install --save-dev attempt-test

Then simply import or require it in your tests:

import attempt from 'attempt-test';

Examples

You can use attempt with any testing framework, but the following examples will use jest and jasmine as the examples. For legibility, the examples will also use async functions, but with jest you can just as easily write return attempt or handle the promise rejection manualy by implementing .catch(...).

This is the most basic example. We have a function that adds two numbers, and we want to know if it always returns the correct result.

import attempt from 'attempt-test';
import add from './add-two-numbers';

it('should add two numbers', async () => (
  attempt((a, b) => (
    add(a.number(), b.number()) === a.number() + b.number()
  ))
));

You can also configure attempt in different ways. See the API section below for details.

import attempt from 'attempt-test';
import slowTask from './slow-task';

it('should run a slow task', async () => (
  attempt((a, b) => {
    // Create a string of random characters of random length
    const string = a.string('*', b.number());

    return slowTask(string) !== null
  }, { timeout: 1000 })
));

API

attempt(function, config);

The main function running the tests. By default it will run 1000 iterations of random values on the function, stopping only for the following reasons:

  • The test function returns false
  • An error is thrown
  • The test successfully passes all iterations

function

The main function. This will be invoked one thousand times by default, running any code you put in it that amount of times.

See the argument section below for an explanation

config

The configuration object accepts the following values:

|Name |Value | |----------|--------------------------------------------------------| |iterations|Number - The amount of iterations to run (default 1000) | |timeout |Number - Milliseconds before the test is aborted |

argument

The test function is passed a set arguments. Each of these arguments are objects which contain value generators for use in your test.

attempt((a) => {
  console.log(a);
  // {
  //   num(),
  //   number(),
  //   str(),
  //   string(),
  // }
});

argument.num(config) | argument.number(config)

Generates a javascript number. By default number generates an integer between 0 and 100, but this is easily configurable by passing a configuration object.

The configuration object recognizes the following values:

|Name |Value | |-------|-------------------------------------------------------------------| |min |Number - The lowest possible number to generate (default 0) | |max |Number - The highest possible number to generate (default 100) | |integer|boolean - Generates a decimal number if set to false (default true)|

const number = a.number({ min: -20, max: 20, integer: false });

argument.str(pattern, length, options) | argument.string(pattern, length, options)

Generates a javascript string. By default it generates a string of all characters with a length of 10, but this is easily configurable by passing arguments to the constructor.

See the randomatic documentation for a detailed description of the input parameters you can pass to string.

The arguments are as follows:

|Name |Value | |-------|----------------------------------------------------------------------| |pattern|String - The format of output (default '*') | |length |Number - The length of the string to generate (default 10) | |options|object - A configuration object. See the randomatic documentation|

const lowercaseString = a.string('a', 31);
const uppercaseString = b.string('A');
const customChars = c.string('?', 20, { chars: 'æøåÆØÅ' });