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.
See Change Log for changes from previous versions. As of version 3.0.0 the minimum version of lab that this package is compatible with is 15.0.0.
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("standardConstructorTest", () => {
testing.standardConstructorTest(TestClass, ["one", "two"], "one", "two");
});
Executes basic tests for nulls and undefined against all constructor object properties.
- class: Class - The class to instantiate
- validParam: Object - a valid argument object
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("destructuredConstructorTest", () => {
testing.destructuredConstructorTest(TestClass, {"one": "one", "two": "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();
});
});
The created function also supports skip
and only
.
group.skip("methodOne", () => {
lab.test(done => {
return done();
});
});
group.only("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 properties of the function parameter object.
- function: Function - The function to test
- validParam: object - valid argument object to pass to the function
import * as Lab from "lab";
import getHelper from "lab-testing";
const lab = exports.lab = Lab.script();
const testing = getHelper(lab);
lab.experiment("functionDestructuredParameterTest", () => {
const fnc = function ({one, two}) {
// no parameter checks! This will fail some tests
return;
};
testing.throws.functionDestructuredParameterTest(fnc, {"one": "one", "two": "two"});
});
import * as Lab from "lab";
import getHelper from "lab-testing";
const lab = exports.lab = Lab.script();
const testing = getHelper(lab);
lab.experiment("functionDestructuredParameterTest", () => {
const fnc = function ({one, two}) {
// no parameter checks! This will fail some tests
return new Promise((resolve, reject) => {
return resolve({one, two});
});
};
testing.rejects.functionDestructuredParameterTest(fnc, {"one": "one", "two": "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");
});
Executes basic tests for nulls and undefined against all properties of the method parameter object.
- object: Object - The instance of a class
- function: Function - The method on that instance
- validParam: object - valid argument object to pass to the method
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("methodDestructuredParameterTest", () => {
const obj = new TestClass();
testing.throws.methodDestructuredParameterTest(obj, obj.method, {"one": "one", "two": "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("methodDestructuredParameterTest", () => {
const obj = new TestClass("one", "two");
testing.rejects.methodDestructuredParameterTest(obj, obj.method, {"one": "one", "two": "two"});
});