npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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
  • msg String The message to log
  • level String The log level to execute (optional, default 'info')
expectFailure

Convenience function that will handle expected thrown errors

Parameters
  • type (String | Array<String>) The flags type/name that will be set on incoming flags (e.g. onUnhandledRejection, onUncaughtException, etc.)
  • opts Object The failure options
    • opts.expect Function The @hapi/code expect function
    • opts.label String? The label that will be used for expect
    • opts.code String? The Error.code that will be expected
  • func 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? true to reject, otherwise resolve
usingTestRunner

Returns Boolean true when the process is being ran from a test utility