Socket
Book a DemoInstallSign in
Socket

@simpleview/mochalib

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@simpleview/mochalib

Mocha helpers to help make common tasks easier.

Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
56
-13.85%
Maintainers
1
Weekly downloads
 
Created
Source

@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(args, fn)

  • args - 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 or async function - Executed as await fn(args), receives the test args returned on args. 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
				}
			}
		]
		
		mochaLib.testArray(tests, function(test) {
			assert.strictEqual(test.num * 2, test.result);
		});
	});
});

FAQs

Package last updated on 20 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts