Pop-Quiz (Formery "Tester")

/js/pop-quiz@0.0.0/index.mjs

A context-independent testing framework inspired by tape.

Comparison to Tape

Like Tape, pop-quiz

Unlike Tape, pop-quiz

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:

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:

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:

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.

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:

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.