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

junit

v1.4.9

Published

A simple promise based function for unit tests.

Downloads

105,080

Readme

JUnit

A simple promise based function for unit tests. I believe we shouldn't waste time on learning, debugging and waiting the unit test framework itself, that's why I created JUnit. It's just a curried function, everything inside is controllable, nothing will be fancy.

NPM version Build Status Deps Up to Date Coverage Status

Install

Node.js

npm install junit then you can var junit = require("junit").default or import junit from "junit".

Browser

You can to use something like browserify or webpack, or download the bundled junit.js. A real world example.

Features

  • Supports both Node.js and old browsers
  • Should work well from ES3 to ES7
  • Make it super easy to concurrently test async functions, designed for async-await
  • Automatically garbage collect the unhandled error
  • Full customizable report style
  • Not a single global variable pollution
  • Only one dependency, light weight and behavior predictable

FAQ

  • I don't want to use async-await.

    No problem. Just replace all the await expresses with standard promise ones is enough.

  • I cannot require('junit').

    For non-es6, use require('junit').default.

  • IE6?

    The core framework of JUnit will work. But the default reporter only supports IE8>=, you may have to install & config to another reporter to support old browsers.

CLI

Install junit globally: npm i -g junit. It will automatically take advantage of the babel if you have installed it globally.

For example, created a file test/fib-test.js, it should export a function, if the function is async it should return a promise, such as:

import sleep from "yaku/lib/sleep";

module.exports = async it => {
    await sleep(3000);

    it("fib 01", () => eq(1 + 1, 2));

    it("fib 02", () => eq(1 + 2, 3));

    it("fib 03", () => eq(2 + 3, 5));
};

Run the tests via junit test/*.js.

For more documentation, run junit -h.

To watch and auto-rerun test please use noe:

noe -b junit -w 'test/*.js' -- 'test/*.js'

junit-demo

API


  • junit(opts)

    A simple promise based module for unit tests.

    • param: opts { Object }

      Defaults:

      {
          filter: (msg) => true
      
          // Stop test when error occurred.
          isBail: true,
      
          isFailOnUnhandled: true,
      
          // If any test failed, throw on final.
          isThrowOnFinal: true,
      
          // Fail a test after timeout.
          timeout: 5000,
      
          reporter: {
              // You can even use jsdiff here to generate more fancy error info.
              formatAssertErr: (actual, expected) => {},
      
              logPass: (msg, span) => {},
              logFail: (msg, err, span) => {},
              logFinal: (total, tested, passed, failed) => {}
          }
      }
    • return: { Function }

      (msg, fn) => Function The msg can be anything. The fn's first param is a function (after) =>, you can pass a after hook to it.

    • example:

      import junit from "junit";
      var it = junit();
      (async () => {
          // Async tests.
          it("test 1", () =>
              // We use `it.eq` to assert on both simple type and complex object.
              it.eq("ok", "ok")
          );
      
          it("test 2", async () => {
              // No more callback hell while testing async functions.
              await new junit.Promise(r => setTimeout(r, 1000));
      
              return it.eq({ a: 1, b: 2 }, { a: 1, b: 2 });
          });
      
          // Run sync tests within the main async flow.
          await it("test 3", (after) =>
              after(() => {
                  // do some clean work after the test
              });
      
              it.eq("ok", "ok")
          );
      
          it.run();
      })();
    • example:

      Filter the tests, only the message starts with "test" will be tested.

      import junit from "junit";
      var it = junit({
          filter: (msg) => msg.indexOf("test")
      });
      
      (async () => {
          it("basic 1", () => it.eq(1, 1));
          it("test 1", () => it.eq(1, 1));
          it("test 2", () => it.eq(1, 1));
      
          // Get the result of the test.
          var { total, tested, passed, failed } = await it.run();
      
          console.log(total, tested, passed, failed);
      })();
  • run()

    Start the tests.

    • return: { Promise }

      It will resolve { total, passed, failed }

  • eq(actual, expected, maxDepth)

    A smart strict deep equality assertion helper function. If any of the arguments is promise, it will be auto-resolved before comparision.

    • param: actual { Any }

    • param: expected { Any }

    • param: maxDepth { Number = 7 }

      Optional. The max depth of the recursion check.

    • return: { Promise }

  • describe(msg, fn)

    Extend the msg of the test with a new test closure.

    • param: msg { Any }

      The msg object of the test.

    • param: fn { Function }

      (it) => Promise The new msg closure.

    • return: { Promise }

    • example:

      import junit from "junit";
      
      var it = junit();
      var { eq } = it;
      
      it.describe("level 01", it => {
          it("test 01", () => eq(1, 1));
      
          it("test 02", () => eq(1, 1));
      
          it.describe("level 02", it => {
              it("test 01", () => eq(1, 1));
      
              it("test 02", () => eq(1, 1));
          });
      });
      
      it.run();
  • junit.reporter(opts)

    An example reporter for junit.

    • param: opts { Object }

      Defaults:

      {
          prompt: String, // The prompt prefix
          mode: "console" // "console", "browser" or "none"
      }
    • return: { Function }

      () => Object.

    • example:

      var it = junit({ reporter: junit.reporter({ prompt: 'my-prompt > ' }) });
  • junit.Promise

    The promise class that junit uses: Yaku

    • type: { Object }
  • junit.yutils

    The promise helpers: Yaku Utils

    • type: { Object }