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

@simpleview/mochalib

v2.0.2

Published

Mocha helpers to help make common tasks easier.

Downloads

433

Readme

@simpleview/mochalib

Getting Started

npm install @simpleview/mochalib
const mochaLib = require("@simpleview/mochalib");

Overview

Often when writing unit tests you will end up writing numerous tests which have the exact same boilerplate. In example if you are testing a templating engine you must end up with a section like follows:

it("should fill with string", function() {
	assert.strictEqual(template.fill("{{data}}", { data : "foo" }), "foo");
});

it("should fill with boolean", function() {
	assert.strictEqual(template.fill("{{data}}", { data : true }), "true");
})

it("should fill with number", function() {
	assert.strictEqual(template.fill("{{data}}", { data : 10 }), "10");
});

In that case all 3 tests are basically doing the same thing. But there's a lot of duplicated code between each test. That can be done cleaner like so:

const tests = [
	{
		name : "should fill with string",
		args : {
			data : "foo",
			result : "foo"
		}
	},
	{
		name : "should fill with boolean",
		args : {
			data : true,
			result : "true"
		}
	},
	{
		name : "should fill with number",
		args : {
			data : 10,
			result : "10"
		}
	}
]

mochaLib.testArray(tests, function(test) {
	assert.strictEqual(template.fill("{{data}}", { data : test.data }), test.result);
});

The more complicated the test, the more savings you get by utilizing a test array.

API

testArray(tests, fn)

  • tests - object[]
    • name - string - Test name
    • timeout - number - Set a timeout in ms for this specific test.
    • before - function or async function - Execute a function prior to execution of the test. Executed as await before(args).
    • after - function or async function - Execute a function after execute of the test. Executed as await after(args).
    • only - boolean - Only execute this test.
    • skip - boolean - Skip this test.
    • args - object or function or async function - Definition of the test data which will be passed to the fn. Can be an object of data, or a function, or an async function.
  • fn - function(test) or async function(test) - Receives the test args returned on test. If you utilize a non-async function, it must return a promise if you go off the event loop.

Basic Example

const mochaLib = require("@simpleview/mochalib");
const assert = require("assert");

describe(__filename, function() {
	describe("test array", function() {
		const tests = [
			{
				name : "test 1",
				args : {
					num : 1,
					result : 2
				}
			},
			{
				name : "test 2",
				args : {
					num : 2,
					result : 4
				}
			},
			{
				name : "test 3",
				// returning the args from a function
				args : () => {
					return {
						num : 3,
						result : 6
					}
				}
			},
			{
				// return args from an async function with shorthand notation
				name : "test 4",
				args : async () => ({
					num : 4,
					result : 8
				})
			}
		]

		mochaLib.testArray(tests, function(test) {
			assert.strictEqual(test.num * 2, test.result);
		});
	});
});

Using with Typescript

import { testArray, TestDef } from "@simpleview/mochalib";
import assert from "assert";

describe(__filename, function() {
	describe("test array", function() {
		interface Test {
			num: number
			result: number
		}

		// ensures type-checking on your test array
		const tests: TestDef<Test>[] = [
			{
				name : "test 1",
				args : {
					num : 1,
					result : 2
				}
			},
			{
				name : "test 2",
				args : {
					num : 2,
					result : 4
				}
			},
			{
				name : "test 3",
				// returning the args from a function
				args : () => {
					return {
						num : 3,
						result : 6
					}
				}
			},
			{
				// return args from an async function with shorthand notation
				name : "test 4",
				args : async () => ({
					num : 4,
					result : 8
				})
			}
		]

		testArray<Test>(tests, function(test) {
			// now test is strictly typed
			assert.strictEqual(test.num * 2, test.result);
		});
	});
});