test-director
An ultra lightweight unit test director for Node.js.
Works well with any assertion library that throws errors, such as the Node.js assert
API and snapshot-assertion
.
Setup
To install from npm run:
npm install test-director --save-dev
Support
- Node.js
^10.17.0 || ^12.0.0 || >= 13.7.0
API
Table of contents
class TestDirector
An ultra lightweight unit test director for Node.js.
Examples
Ways to import
.
import { TestDirector } from 'test-director';import TestDirector from 'test-director/public/TestDirector.js';
Ways to require
.
const { TestDirector } = require('test-director');const TestDirector = require('test-director/public/TestDirector');
How to construct a new test director.
const tests = new TestDirector();
TestDirector instance method add
Adds a test.
Parameter | Type | Description |
---|---|---|
name |
string | Unique test name. |
test |
Function | Test to run; may return a Promise . |
Examples
A sync test.
const { equal } = require('assert'); const { TestDirector } = require('test-director'); const tests = new TestDirector(); tests.add('JavaScript addition.', () => { equal(1 + 1, 2); }); tests.run();
An async test.
const { ok } = require('assert'); const fetch = require('node-fetch'); const { TestDirector } = require('test-director'); const tests = new TestDirector(); tests.add('GitHub is up.', async () => { const { ok } = await fetch('https://github.com'); ok(ok); }); tests.run();
TestDirector instance method run
Runs the tests one after another, in the order they were added.
Parameter | Type | Description |
---|---|---|
throwOnFailure |
boolean? = false
|
After tests run, throw an error if some failed. |
Returns: Promise<void> — Resolves once tests have run.
Examples
Run nested tests.
const { TestDirector } = require('test-director'); const tests = new TestDirector(); tests.add('Test A.', async () => { const tests = new TestDirector(); tests.add('Test B.', () => { // … }); tests.add('Test C.', () => { // … }); await tests.run(true); }); tests.add('Test D.', () => { // … }); tests.run();
TestDirector instance property tests
A map of test functions that have been added, keyed by their test names.
Type: Map<string, Function>