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

fvb

v0.1.0

Published

TAP producer for JavaScript unit testing

Downloads

6

Readme

fvb

fvb logo

a TAP producer for JavaScript unit testing

getting started

Testing a syncronous function

// add.spec.js

import { test } from "fvb"
import { add } from "./add.js"

test("Test add function", t => {
  t.equal(add(1, 2, 3, 4, 5, 6, 7), 28, "given multiple arguments")
  t.equal(add([1, 2, 3]), 6, "given an array")

  // t.plan is an OPTIONAL assertion that verifies the assertion count
  t.plan(2)
})

Running the test with node.js

node add.spec.js

asyncronous example

Testing async functions

// test-a.js

import { test } from "fvb"

test("Testing another async function in a different file", async t => {
  t.equal(await Promise.resolve(parseInt("1001001", 2)), 73)
  t.notEqual(await Promise.resolve(0), -0)
  t.plan(2)
})

Running the test

node test-a.js

When calling the test function multiple times in a single file and testing async code, top-level await should be used for the tests to run in order and for t.plan to work properly.

// test-b.js

import { test } from "fvb"

await test("Testing async function", async t => {
  t.equal(await Promise.resolve("Hello"), "Hello")
  t.plan(1)
})

await test("Testing another async function", async t => {
  t.equal(await Promise.resolve(42), 42)
  t.plan(1)
})

Running the test

node test-b.js

Use top-level await to ensure that the t.plan method works and the tests run in the expected order.

// index.spec.js

await import("./test-a.js")
await import("./test-b.js")

Running the tests

node index.spec.js

install

pnpm

pnpm add --save-dev fvb

npm

npm install --save-dev fvb

yarn

yarn add --dev fvb

API

test(description, fn)

test :: String -> (T -> Undefined) -> Promise(T)

The test method is the only function provided by fvb. It accepts a string value for the test description as the first argument. The second argument is a function that accepts a T interface as described in the Assert section below. A Promise containing T is returned.

Assert (T)

t.equal(actual, expected, [msg])

t#equal :: a -> b -> (String | Undefined) -> Undefined

A [deep] equality assertion that checks if actual is equal to expected using JavaScript's Object.is static method. Setoid objects with an equals or fantasy-land/equals method are compared using these methods.

t.notEqual(actual, expected, [msg])

t#notEqual :: a -> b -> (String | Undefined) -> Undefined

This assertion is the same as the t.equal method except the values compared are expected to be NOT equal.

t.ok(value, [msg])

t#ok :: a -> (String | Undefined) -> Undefined

Pass if the given value is true.

t.notOk(value, [msg])

t#notOk :: a -> (String | Undefined) -> Undefined

Pass if the given value is false.

t.throws(fn, [msg])

t#throws :: (() -> Undefined) -> (String | Undefined) -> Undefined

Pass if the given function throws when called.

t.doesNotThrow(fn, [msg])

t#doesNotThrow :: (() -> Undefined) -> (String | Undefined) -> Undefined

Pass if the given function does NOT throw when called.

t.plan(n)

t#plan :: Integer -> Undefined

The t.plan module is NOT required. It is just another check that can be used to help ensure all of the assertions ran. The integer given should be a count of the assertions in the current test (excluding the current t.plan call).

When using this method for testing asynchronous functions, be sure to await any async calls before calling t.plan.

t.fail([msg], [actual], [expected])

t#fail :: (String | Undefined) -> a -> b -> Undefined

An assertion that automatically fails. Useful as a helper to build custom assertions.

t.pass([msg])

t#pass :: (String | Undefined) -> Undefined

An assertion that automatically passes. Useful as a helper to build custom assertions.

t.comment(msg)

t#comment :: String -> Undefined

Print the given message as a comment in the TAP output.

t.bail([msg])

t#bail :: (String | Undefined) -> Undefined

Bail out of the test! If the environment has a process.exit method then it is called, otherwise an Error is thrown.

.then(fn)

test returns a Promise containing T. This can be useful for creating custom reporting and for using in the browser's console.

using in the browser

import { test } from "https://cdn.skypack.dev/fvb"

const element = document.querySelector("#test-element")

test("Element exists", t => {
  t.notEqual(element, null, "#test-element should exist in document")
  t.plan(1)
})
.then(t => {
  console.log("TAP version 14")
  console.log(`1..${t.total}`)
  console.log(t.body)
})

References & Alternatives