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

@siteimprove/alfa-test

v0.92.0

Published

A simple test library sitting on top of the built-in Node.js assert module

Downloads

268

Readme

Alfa test

Thanks to the referential transparency ensured by ADR 6, unit test of Alfa code is usually very easy, simply comparing the actual result with the expected one (often as their serialisation), without need for complex setup, mocks, or other test tricks.

We're therefore implementing a very lightweight wrapper for tests.

import { test } from "@siteimprove/alfa-test";

test("My test", (t) => {
  const actual = …;

  t.deepEqual(actual.toJSON(), { type: …, …})
});

Property testing

Sometimes, it is convenient to generate random tests with random values. The alfa-test library is offering test controller to handle that.

  • The assertion function that is passed to test(name, assertion) receives additional rng and seed parameters. The rng is a function () => number. The seed was used to initialize the Random Number Generator, can be used for better displaying errors and for re-playability.
  • The test function itself accepts an optional Controller object which can be used to set the seed for the RNG, or to change the number of iterations to run the test (default to 1 since most tests are not random tests). The Controller object also accepts a wrapper function of type (iteration: number, rng: RNG) => RNG that can be used to turn the random numbers into useful data, or for introspection.

The provided rng function is guaranteed to generate the same sequence of numbers on sequential calls, if the same seed is provided by the controller. If no seed is provided, a random one will be used.

By default, each test is only run once. Use the Controller object to change the number of iterations.

Tests that make use of the RNG are encouraged to print the seed in their error message in order to allow re-playability and investigation by feeding the failing seed back to the test.

For re-playability, use the Controller parameter to select the seed to use (which guarantees the exact same sequence of numbers is produced), and to introspect on fine details by wrapping the RNG, e.g.,

/**
 * Return a random string between "0" and "100" (inclusive).
 * Print the generated number, as well as the iteration number (use for debugging).
 */
function wrapper(rng: RNG<number>, iteration: number): RNG<string> {
  return () => {
    const res = rng();
    console.log(`On iteration ${iteration}, I generated ${res}`);
    return `${res * 100}`;
  };
}

test(
  "Sum computes the sum of two numbers represented as strings",
  (t, rng, seed) => {
    // These use the post-wrapper RNG.
    const a = rng();
    const b = rng();
    // Print the seed in error message to allow introspection.
    const actual = sum(a, b, `Failed with seed ${seed}`);

    t.deepEqual(actual, `${a + b}`);
  },
  {
    wrapper,
    iterations: 100,
    // Set the seed for debugging, if you want to replay the same sequence of numbers.
    seed: 1234,
  },
);