Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ugate/labrat
🐁 Run @hapi/lab tests on vanilla test suites
https://github.com/ugate/labrat
Last synced: 9 days ago
JSON representation
🐁 Run @hapi/lab tests on vanilla test suites
- Host: GitHub
- URL: https://github.com/ugate/labrat
- Owner: ugate
- License: mit
- Created: 2019-12-10T18:22:26.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T06:54:25.000Z (almost 2 years ago)
- Last Synced: 2024-11-02T02:34:33.091Z (13 days ago)
- Language: JavaScript
- Homepage:
- Size: 1.43 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🐭 @ugate/labrat
🐁 Run [@hapi/lab](https://github.com/hapijs/lab) tests on vanilla test suites
`npm install -D @ugate/labrat`
**test/lib/tester.js**:
```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**:
```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](https://github.com/hapijs/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](https://github.com/hapijs/lab):
`"node_modules/.bin/lab" test/tester.js -v`
Run _myTest1_ in [@hapi/lab](https://github.com/hapijs/lab):
`"node_modules/.bin/lab" test/tester.js -vi 1`
Run _myTest2_ in [@hapi/lab](https://github.com/hapijs/lab):
`"node_modules/.bin/lab" test/tester.js -vi 2`
#### API
###### Table of Contents
- [header](#header)
- [Parameters](#parameters)
- [expectFailure](#expectfailure)
- [Parameters](#parameters-1)
- [wait](#wait)
- [Parameters](#parameters-2)
- [usingTestRunner](#usingtestrunner)##### header
Logs a message with header formatting
###### Parameters
- `msg` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The message to log
- `level` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The log level to execute (optional, default `'info'`)##### expectFailure
Convenience function that will handle expected thrown errors
###### Parameters
- `type` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>)** The `flags` type/name that will be set on incoming flags (e.g. `onUnhandledRejection`, `onUncaughtException`, etc.)
- `opts` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The failure options
- `opts.expect` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** The `@hapi/code` expect function
- `opts.label` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** The label that will be used for `expect`
- `opts.code` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** The `Error.code` that will be expected
- `func` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** A _test_ function with a signature of `async 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/rejecting
- `val` **any?** The value to return when resolved or error message/Error when rejecting
- `rejectIt` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** `true` to reject, otherwise resolve##### usingTestRunner
Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` when the process is being ran from a _test utility_