Tester - uhop/tape-six GitHub Wiki
Tester
helps to do asserts and provides an interface between a test suite and the test harness.
Available tests
The following methods are available (all msg
arguments are optional):
- Asserts:
pass(msg)
— asserts that the test passed.fail(msg)
— asserts that the test failed.ok(val, msg)
— asserts thatval
is truthy.true()
— an alias ofok()
.assert()
— an alias ofok()
.
notOk(val, msg)
— asserts thatval
is falsy.false()
— an alias ofnotOk()
.notok()
— an alias ofnotOk()
.
error(err, msg)
— asserts thaterr
is falsy.ifError()
— an alias oferror()
.ifErr()
— an alias oferror()
.iferror()
— an alias oferror()
.
strictEqual(a, b, msg)
— asserts thata
andb
are strictly equal.- Strict equality is defined as
a === b
. is()
— an alias ofstrictEqual()
.equal()
— an alias ofstrictEqual()
.isEqual()
— an alias ofstrictEqual()
.equals()
— an alias ofstrictEqual()
.strictEquals()
— an alias ofstrictEqual()
.
- Strict equality is defined as
notStrictEqual(a, b, msg)
— asserts thata
andb
are not strictly equal.not()
— an alias ofnotStrictEqual()
.notEqual()
— an alias ofnotStrictEqual()
.notEquals()
— an alias ofnotStrictEqual()
.notStrictEquals()
— an alias ofnotStrictEqual()
.doesNotEqual()
— an alias ofnotStrictEqual()
.isUnequal()
— an alias ofnotStrictEqual()
.
looseEqual(a, b, msg)
— asserts thata
andb
are loosely equal.- Loose equality is defined as
a == b
. looseEquals()
— an alias oflooseEqual()
.
- Loose equality is defined as
notLooseEqual(a, b, msg)
— asserts thata
andb
are not loosely equal.notLooseEquals()
— an alias ofnotLooseEqual()
.
deepEqual(a, b, msg)
— asserts thata
andb
are deeply equal.- Individual components of
a
andb
are compared recursively using the strict equality. - See deep6's equal() for details.
same()
— an alias ofdeepEqual()
.deepEquals()
— an alias ofdeepEqual()
.isEquivalent()
— an alias ofdeepEqual()
.
- Individual components of
notDeepEqual(a, b, msg)
— asserts thata
andb
are not deeply equal.notSame()
— an alias ofnotDeepEqual()
.notDeepEquals()
— an alias ofnotDeepEqual()
.notEquivalent()
— an alias ofnotDeepEqual()
.notDeeply()
— an alias ofnotDeepEqual()
.isNotDeepEqual()
— an alias ofnotDeepEqual()
.isNotEquivalent()
— an alias ofnotDeepEqual()
.
deepLooseEqual(a, b, msg)
— asserts thata
andb
are deeply loosely equal.- Individual components of
a
andb
are compared recursively using the loose equality.
- Individual components of
notDeepLooseEqual(a, b, msg)
— asserts thata
andb
are not deeply loosely equal.throws(fn, msg)
— asserts thatfn
throws.fn
is called with no arguments in the global context.
doesNotThrow(fn, msg)
— asserts thatfn
does not throw.matchString(string, regexp, msg)
— asserts thatstring
matchesregexp
.doesNotMatchString(string, regexp, msg)
— asserts thatstring
does not matchregexp
.match(a, b, msg)
— asserts thata
matchesb
.- See deep6's match() for details.
doesNotMatch(a, b, msg)
— asserts thata
does not matchb
.rejects(promise, msg)
— asserts thatpromise
rejects.- This is an asynchronous method. It is likely to be waited for.
doesNotResolve()
— an alias ofrejects()
.
resolves(promise, msg)
— asserts thatpromise
resolves.- This is an asynchronous method. It is likely to be waited for.
doesNotReject()
— an alias ofresolves()
.
- Embedded test suites (all of them are asynchronous and should be waited for):
test(name, options, testFn)
— runs a test suite asynchronously. Seetest()
above.skip(name, options, testFn)
— skips a test suite asynchronously. Seetest.skip()
above.todo(name, options, testFn)
— runs a provisional test suite asynchronously. Seetest.todo()
above.asPromise(name, options, testPromiseFn)
— runs a test suite asynchronously. Seetest.asPromise()
above.
- Miscellaneous:
any
— returns theany
object. It can be used in deep equivalency asserts to match any value. See deep6's any for details.plan(n)
— sets the number of tests in the test suite. Rarely used.comment(msg)
— sends a comment to the test harness. Rarely used.skipTest(...args, msg)
— skips the current test yet sends a message to the test harness.bailOut(msg)
— stops the test suite and sends a message to the test harness.
In all cases, the msg
message is optional. If it is not provided, some suitable generic message will be used.
Examples
test('Sample test', async t => {
const result = await getFromDb({first: 'Bob', last: 'Smith'});
t.equal(result.position, 'chief bozo', 'the position is correct');
t.ok(result.manager, 'the manager exists');
const manager = await getFromDb(result.manager);
t.ok(manager, 'the manager is retrieved');
t.equal(manager.first, 'Jane', 'the manager is Jane');
t.deepEqual(manager.employees, ['Bob Smith'], 'Jane manages only Bob Smith');
});