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

lab-testing

v4.2.0

Published

Some little time savers for testing with Code and Lab

Downloads

6

Readme

lab-testing

Greenkeeper badge

Build Status Coverage Status Deps devDependency Status

NPM

Installation

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.

Standard Constructor Test

Executes basic tests for nulls and undefined against all constructor parameters.

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");

});

Destructured Constructor Test

Executes basic tests for nulls and undefined against all constructor object properties.

Parameters:

  • 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"});

});

Create Experiment

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.

Parameters:

  • ...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();
  });

});

Function Parameter Test

Executes basic tests for nulls and undefined against all function parameters.

Parameters:

  • function: Function - The function to test
  • labels: string[] - description of the parameters for the constructor
  • parameters: ...params - The correct values for the constructor

Testing for Thrown Exceptions

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");

});

Testing for Rejected Promises

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");

});

Function Destructured Parameter Test

Executes basic tests for nulls and undefined against all properties of the function parameter object.

Parameters:

  • function: Function - The function to test
  • validParam: object - valid argument object to pass to the function

Testing for Thrown Exceptions

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"});

});

Testing for Rejected Promises

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"});

});

Method Parameter Test

Executes basic tests for nulls and undefined against all method parameters.

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

Testing for Thrown Exceptions

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");

});

Testing for Rejected Promises

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");

});

Method Destructured Parameter Test

Executes basic tests for nulls and undefined against all properties of the method parameter object.

Parameters:

  • object: Object - The instance of a class
  • function: Function - The method on that instance
  • validParam: object - valid argument object to pass to the method

Testing for Thrown Exceptions

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"});

});

Testing for Rejected Promises

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"});

});