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

pop-quiz

v0.0.6

Published

Modern Javascript that works in all enviornments with zero dependencies

Downloads

4

Readme

Pop-Quiz (Formery "Tester")

/js/[email protected]/index.mjs

A context-independent testing framework inspired by tape.

Comparison to Tape

Like Tape, pop-quiz

Unlike Tape, pop-quiz

  • can be run directly in the browser or using node, deno, and browser environments without any binaries or transformations.
  • requires no dependencies
  • uses external assertions and makes it easy to write your own.

context-agnostic

Tests run in same context as your application. No special executables needed.

TAP Output

Pop-Quiz outputs to the console using a partial implementation of the Test Anything Protocol.

Installation

Install via npm with npm install pop-quiz or import directly from website:

import quiz from "https://johnhenry.github.io/lib/js/pop-quiz/0.0.0/index.mjs";

API

Pop-Quiz's API consist of two manin components:

  • The "quiz" function acts on a group of assertions.
  • The assertions themselves, which return errors if a given condition is not satisfied.

Quiz

The quiz function is the default export.

It takes as its only argument a [possibly asynchronous] generator. We call this a "test".

Results of assertions are yielded from within the body of a test.

import quiz from "pop-quiz";
quiz(function* () {
  yield /*some assertion result*/;
  yield /*some other assertion result*/;
});

Assertions

The named exports are assertions.

Call them within a test and yield their results.

import quiz, { ok, notok } from "pop-quiz";

quiz(function* () {
  yield ok(true);
  yield notok(false);
});

Included Assertions

Besides ok and notok, a number of assertions are included:

  • ok -- test passes if and only if the given argument to a test is TRUTHY.
  • notok -- test passes if and only if the given argument to a test is FALSH.
  • equal -- test passes if and only if the two given arguments are THE SAME object.
  • notequal -- test passes if and only if the two given arguments are NOT THE SAME object.
  • deepequal -- test passes if and only if two objects are deeply equal.
  • pass -- test ALWAYS PASSES
  • fail -- test ALWAYS FAILS
  • subtestpass -- test passes if and only if the given argument is a test in which ALL THE ASSERTIONS PASS.
  • subtestfail -- test passes if and only if the given argument is a test in which AT LEAST ONE ASSERTION FAILS.
  • throws -- test passes if and only if the given function THROWS AN ERROR when called
  • doesnotthrow -- test passes if and only if the given function DOES NOT THROW AN ERROR when called

plan

When using the run function, the first argument passed to given generator is a function. We'll call it "plan", but you can name it anyting you like ("expect", "assertions", etc.) When plan is called with an integer, it dictates the number of expected assertions in a given test function.

import quiz, { ok } from "pop-quiz";

quiz(function* (plan) {
  plan(1);
  yield ok(true);
});

Creating Assertions

When creating assertions, use the examples in ./assertions for inspiration. Here are a few things to keep in mind:

  • Assertions are functions that test for a desired conditon.
  • If the given conditions meet the desired conditions,
    • an accepted message is returned.
    • Otherwise, an instance of TestError is returned.
import TestError from "./testerror.mjs";

const assertion = (/*given conditions*/)=>{
  if(/*conditions are met*/){
    return /*some message*/;
  }
  return new TestError(/*some message*/);
}

Conventions

This library follows a specific convetion for its assertions. It's recommended that you follow these conventions when creating your own assertions, but feel free to come up with your own.

  • The last item is an operator string, which is used for the TAP protocol and can be overridden.
  • The next-to-last item is a default expected message that can also be overridden.
  • The preceeding arguments are given conditions to be tested.
  • The returned TestError is constructed using the default expected message along with an object detailing the difference between what's expected and what's not.
import TestError from "./testerror.mjs";

const assertion = (/*given conditions*/, message, operatorString)=>{
  if(/*conditions are met*/){
    return message;
  }
  return new TestError(message, /*some object*/));
}

TestError API

The test error is constructed with two items:

  • An expected messages
  • An object who's key-value pairs are displayed as part of TAP output

TAPRunner, print, run

The file "/TAPRunner.mjs" export methods "print" and "run". "print" functions similarly to the default export of "index.mjs" -- both of which rely on "run" to execute underlying code.

When called with as single argument (a test), "run" yields only the results of the test (string or Error) without additional processing.