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

js-test-utils

v0.3.1

Published

A collection of utilities for JavaScript testing.

Downloads

3

Readme

js-test-utils

This package contains various utility methods that are useful for testing in a JavsScript environment.

Installation

npm install js-test-utils

Usage

Require the package

const TestUtil = require('js-test-utils');

and use any of the methods outlined below

Method Reference

IdentityClass

IdentityClass is a class property. Instantiating the class with a single argument passed to the constructor will set the instance's value property to the value of that argument.

const TestUtil = require('js-test-utils');

const myClass1 = new TestUtil.IdentityClass('Tax Loss Harvesting');
myClass1.value; // 'Tax Loss Harvesting

assertThrows

assertThrows takes a function reference and an expected error message, and asserts that the function throws an error. If an expected error message was provided, it will assert that the actual error message matches (deep equal).

const TestUtil = require('js-test-utils');

TestUtil.assertThrows(() => throw new Error('CAC 40'), 'CAC 40');
// This will not throw an error

TestUtil.assertThrows(() => throw new Error('CAC 40'));
// This will not throw an error

TestUtil.assertThrows(() => throw new Error('CAC 40'), 'ASX 100');
// This will throw an error

TestUtil.assertThrows(() => 'CAC 40', 'CAC 40');
// This will throw an error

assertDoesNotThrow

assertDoesNotThrow takes a function reference and an expected result, and asserts that the function does not throw an error. If an expected result was provided, it will assert that the actual result matches (deep equal).

const TestUtil = require('js-test-utils');

TestUtil.assertDoesNotThrow(() => 'CAC 40', 'CAC 40');
// This will not throw an error

TestUtil.assertDoesNotThrow(() => 'CAC 40');
// This will not throw an error

TestUtil.assertDoesNotThrow(() => 'CAC 40', 'ASX 100');
// This will throw an error

TestUtil.assertDoesNotThrow(() => throw new Error('CAC 40'), 'CA 40');
// This will throw an error

expectPromiseRejection

expectPromiseRejection takes a function which returns a promise and an expected rejection message, and asserts that the promise rejects. If an expected rejection message was provided, it will assert that the actual rejection message matches (deep equal). expectPromiseRejection returns a promise.

const TestUtil = require('js-test-utils');

TestUtil.expectPromiseRejection(() => Promise.reject('ASX 100'), 'ASX 100');
// This will resolve

TestUtil.expectPromiseRejection(() => Promise.reject('ASX 100'));
// This will resolve

TestUtil.expectPromiseRejection(() => Promise.reject('ASX 100'), 'CAC 40');
// This will reject

TestUtil.expectPromiseRejection(() => Promise.resolve('ASX 100'), 'ASX 100');
// This will reject

expectPromiseResolution

expectPromiseResolution takes a function which returns a promise and an expected resolution, and asserts that the promise resolves. If an expected resolution was provided, it will assert that the actual resolution matches (deep equal). expectPromiseResolution returns a promise.

const TestUtil = require('js-test-utils');

TestUtil.expectPromiseResolution(() => Promise.resolve('ASX 100'), 'ASX 100');
// This will resolve

TestUtil.expectPromiseResolution(() => Promise.resolve('ASX 100'));
// This will resolve

TestUtil.expectPromiseResolution(() => Promise.resolve('ASX 100'), 'CAC 40');
// This will reject

TestUtil.expectPromiseResolution(() => Promise.reject('ASX 100'), 'ASX 100');
// This will reject

mapValuesExcept

mapValuesExcept takes an initial object, variable to map to, and a list of exceptions. It returns a new object with the same keys as the initial object, but the values for the keys not in the exceptions array have been replaced by a copy of the variable to map to.

const TestUtil = require('js-test-utils');

TestUtil.mapValuesExcept({
    notes: 'foo',
    accounts: 13,
    salaries: undefined,
    wages: 14.01,
}, { an: 'object' }, ['salaries', 'wages', 'interest', 'taxes']);

/* {
 *     notes: { an: 'object' },
 *     accounts: { an: 'object' },
 *     salaries: undefined,
 *     wages: undefined,
 * }

suppressTestOutput

suppressTestOutput stubs properties on the global console object to prevent output. It takes a function reference and an optional list of console output types to stub, which defaults to ['log']. Note that because this method stubs methods on the console object, output via alternative methods (such as process.stdout.write) will not be suppressed.

const TestUtil = require('js-test-utils');

TestUtil.suppressTestOutput(() => {
    console.log('foo');
    console.warn('bar');
    console.error('baz');
}, ['log', 'warn']);
// Only 'baz' will be displayed in the console.

testClassIsNotInstantiable

testClassIsNotInstantiable attempts to instantiate the given class with one argument: {}. It asserts that this throws the error "${ClassRef.name} is not instantiable."

const TestUtil = require('js-test-utils');

class NonInstantiableClass1 {
    constructor (obj) {
        throw new Error('NonInstantiableClass1 is not instantiable');
    }
}

class NonInstantiableClass2 {
    constructor (obj) {
        throw new Error('Another error message');
    }
}

TestUtil.testClassIsNotInstantiable(NonInstantiableClass1);
// This will not throw an error

TestUtil.testClassIsNotInstantiable(NonInstantiableClass2);
// This will throw an error

TestUtil.testClassIsNotInstantiable(class {});
// This will throw an error

testSubclassIsInstantiable

testClassIsNotInstantiable attempts to instantiate a subclass of the given class with one argument: {}. It asserts that this does not throw an error. By default, the subclass is automatically derived, although it can also be explicitly provided as the second argument.

const TestUtil = require('js-test-utils');

class NonInstantiableClass1 {
    constructor (obj) {
        throw new Error('NonInstantiableClass1 is not instantiable');
    }
}

class NonInstantiableClass2 {
    constructor (obj) {
        if ('NonInstantiableClass2' === this.constructor.name)
            throw new Error('Another error message');
    }
}

TestUtil.testSubclassIsInstantiable(NonInstantiableClass1, class {});
// This will not throw an error

TestUtil.testSubclassIsInstantiable(NonInstantiableClass2);
// This will not throw an error

TestUtil.testSubclassIsInstantiable(NonInstantiableClass1);
// This will throw an error