@ugate/labrat
v2.1.0
Published
Run @hapi/lab tests on vanilla test suites
Downloads
5
Readme
🐭 @ugate/labrat
🐁 Run @hapi/lab tests on vanilla test suites
npm install -D @ugate/labrat
test/lib/tester.js:
// vanilla test suite (all tests should be static)
const { Labrat, LOGGER } = require('@ugate/labrat');
class Tester {
static async before() {
// OPTIONAL: run before all tests
}
static async after() {
// OPTIONAL: run after all tests
}
static async beforeEach() {
// OPTIONAL: run before each test
}
static async afterEach() {
// OPTIONAL: run after each test
}
static myTest1() {
if (LOGGER.info) LOGGER.info('Show this when info level enabled');
// test here
}
static async myTest2() {
Labrat.header('My Test #2');
// test here
}
static async testException() {
// do something that throws an error
throw new Error('TEST Error');
}
}
// when not ran in a test runner execute static Tester static functions
if (!Labrat.usingTestRunner()) {
(async () => await Labrat.run(Tester))();
}
test/tester.js:
const Lab = require('@hapi/lab');
const Tester = require('./lib/tester');
const lab = Lab.script();
exports.lab = lab;
const plan = `Demo`;
lab.experiment(plan, () => {
if (Tester.before) lab.before(Tester.before);
if (Tester.after) lab.after(Tester.after);
if (Tester.beforeEach) lab.beforeEach(Tester.beforeEach);
if (Tester.afterEach) lab.afterEach(Tester.afterEach);
lab.test(`${plan}: Test #1`, { timeout: 1000 }, Tester.myTest1);
lab.test(`${plan}: Test #2`, { timeout: 1000 }, Tester.myTest2);
lab.test(`${plan}: Test Error`, { timeout: 1000 },
Labrat.expectFailure('onUnhandledRejection', { expect, label: 'throw error' }, Tester.testException)
);
});
Log Levels
-NODE_ENV=development
or-NODE_ENV=dev
- All levels/functions included in console-NODE_ENV=test
- Includes console.info, console.warn, console.error-NODE_ENV=production
or-NODE_ENV=prod
- Includes console.warn, console.error- Omit or set to another environment to disable logging
Running Tests
Tests can be ran in a Node.js
command or in @hapi/lab.
Run in node:
node test/lib/tester.js -NODE_ENV=test
Run myTest1 in node:
node test/lib/tester.js -NODE_ENV=test myTest1
Run in @hapi/lab:
"node_modules/.bin/lab" test/tester.js -v
Run myTest1 in @hapi/lab:
"node_modules/.bin/lab" test/tester.js -vi 1
Run myTest2 in @hapi/lab:
"node_modules/.bin/lab" test/tester.js -vi 2
API
Table of Contents
header
Logs a message with header formatting
Parameters
expectFailure
Convenience function that will handle expected thrown errors
Parameters
type
(String | Array<String>) Theflags
type/name that will be set on incoming flags (e.g.onUnhandledRejection
,onUncaughtException
, etc.)opts
Object The failure optionsfunc
Function A test function with a signature ofasync function(flags)
that@hapi/lab
accepts
wait
Async test that will either resolve
/reject
after a given amount of time
Parameters
delay
Integer The delay in milliseconds to wait before resolving/rejectingval
any? The value to return when resolved or error message/Error when rejectingrejectIt
Boolean?true
to reject, otherwise resolve
usingTestRunner
Returns Boolean true
when the process is being ran from a test utility