npm install lab-testing --save-dev
lab-testing contains two namespaces: throws
and rejects
. The contain the same tests with throws
used to test synchronous messages and rejects
used to test promises. In addition, there are a few top levels tests too.
Executes basic tests for nulls and undefined against all constructor parameters.
- class: Class - The class to instantiate
- labels: string[] - description of the parameters for the constructor
- parameters: ...params - The correct values for the constructor
import * as Code from "code";
import * as Lab from "lab";
import getHelper from "lab-testing";
const lab = exports.lab = Lab.script();
const testing = getHelper(lab);
lab.experiment("standardContructorTest", () => {
testing.standardContructorTest(TestClass, ["one", "two"], "one", "two");
});
Sometimes you want to represent hierarchy in your tests which, with lab, means a lot of indenting. This just reduces that indent and eliminates the boilerplate code.
- ...levels: string[] - Any number of levels as strings
import * as Code from "code";
import * as Lab from "lab";
import getHelper from "lab-testing";
const lab = exports.lab = Lab.script();
const expect = Code.expect;
const testing = getHelper(lab);
const group = testing.createExperiment("Service", "Component");
group("methodOne", () => {
lab.test(done => {
return done();
});
});
Executes basic tests for nulls and undefined against all function parameters.
- function: Function - The function to test
- labels: string[] - description of the parameters for the constructor
- parameters: ...params - The correct values for the constructor
import * as Lab from "lab";
import getHelper from "lab-testing";
const lab = exports.lab = Lab.script();
const testing = getHelper(lab);
lab.experiment("functionParameterTest", () => {
const fnc = function (one, two) {
// no parameter checks! This will fail some tests
return;
};
testing.throws.functionParameterTest(fnc, ["one", "two"], "one", "two");
});
import * as Lab from "lab";
import getHelper from "lab-testing";
const lab = exports.lab = Lab.script();
const testing = getHelper(lab);
lab.experiment("functionParameterTest", () => {
const fnc = function (one, two) {
// no parameter checks! This will fail some tests
return new Promise((resolve, reject) => {
return resolve({one, two});
});
};
testing.rejects.functionParameterTest(fnc, ["one", "two"], "one", "two");
});
Executes basic tests for nulls and undefined against all method parameters.
- object: Object - The instance of a class
- function: Function - The method on that instance
- labels: string[] - description of the parameters for the constructor
- parameters: ...params - The correct values for the constructor
import * as Lab from "lab";
import getHelper from "lab-testing";
const lab = exports.lab = Lab.script();
const testing = getHelper(lab);
class TestClass {
method(one, two) {
// no parameter checks! This will fail some tests
return;
}
}
lab.experiment("methodParameterTest", () => {
const obj = new TestClass();
testing.throws.methodParameterTest(obj, obj.method, ["one", "two"], "one", "two");
});
import * as Lab from "lab";
import getHelper from "lab-testing";
const lab = exports.lab = Lab.script();
const testing = getHelper(lab);
class TestClass {
method(one, two) {
// no parameter checks! This will fail some tests
return new Promise((resolve, reject) => {
return resolve({one, two});
});
}
}
lab.experiment("methodParameterTest", () => {
const obj = new TestClass("one", "two");
testing.rejects.methodParameterTest(obj, obj.method, ["one", "two"], "one", "two");
});