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