Tester - uhop/tape-six GitHub Wiki
Tester helps to do asserts and provides an interface between a test suite and the test harness.
Available properties
signal— a signal (AbortSignal) to cancel asynchronous operations.- It is triggered when the test is stopped or timed out.
- It is useful to control potentially run-away asynchronous operations.
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 thatvalis truthy.true()— an alias ofok().assert()— an alias ofok().
notOk(val, msg)— asserts thatvalis falsy.false()— an alias ofnotOk().notok()— an alias ofnotOk().
error(err, msg)— asserts thaterris falsy.ifError()— an alias oferror().ifErr()— an alias oferror().iferror()— an alias oferror().
strictEqual(a, b, msg)— asserts thataandbare 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 thataandbare 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 thataandbare loosely equal.- Loose equality is defined as
a == b. looseEquals()— an alias oflooseEqual().
- Loose equality is defined as
notLooseEqual(a, b, msg)— asserts thataandbare not loosely equal.notLooseEquals()— an alias ofnotLooseEqual().
deepEqual(a, b, msg)— asserts thataandbare deeply equal.- Individual components of
aandbare 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 thataandbare 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 thataandbare deeply loosely equal.- Individual components of
aandbare compared recursively using the loose equality.
- Individual components of
notDeepLooseEqual(a, b, msg)— asserts thataandbare not deeply loosely equal.throws(fn, msg)— asserts thatfnthrows.fnis called with no arguments in the global context.
doesNotThrow(fn, msg)— asserts thatfndoes not throw.matchString(string, regexp, msg)— asserts thatstringmatchesregexp.doesNotMatchString(string, regexp, msg)— asserts thatstringdoes not matchregexp.match(a, b, msg)— asserts thatamatchesb.- See deep6's match() for details.
doesNotMatch(a, b, msg)— asserts thatadoes not matchb.rejects(promise, msg)— asserts thatpromiserejects.- This is an asynchronous method. It is likely to be waited for.
doesNotResolve()— an alias ofrejects().
resolves(promise, msg)— asserts thatpromiseresolves.- 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 theanyobject. 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.
- Evaluators:
OK(condition, msg, options)— a high-level helper for evaluating simple expressions. It takes a code snippet as a string and returns a code snippet as a string that checks the condition when evaluated witheval().- Available since 1.4.0.
- Aliases:
TRUE(),ASSERT(). - Arguments:
conditionis a simple JavaScript expression (a code snippet) as a string.msgis a string that describes the condition.optionsis an object that can contain additional options. Right now the only supported option isself— a string that represents the name of the tester object. Default:"t".
- Return value: a code as a string that checks the condition when evaluated with
eval(). - Supported signatures:
OK(condition[, msg[, options]])OK(condition, options)
- If
msgis not provided,conditionwill be used. - If fails, it provides a dictionary of all top-level variables in the
conditionand their values helping to see what went wrong.
- Hooks (see before and after hooks for more information):
beforeEach(fn)— a function that is called before each test in the current scope.afterEach(fn)— a function that is called after each test in the current scope.beforeAll(fn)— a function that is called before all tests in the current scope.afterAll(fn)— a function that is called after all tests in the current scope.before(fn)— an alias forbeforeAll(fn).after(fn)— an alias forafterAll(fn).
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');
});
Examples on using OK() (AKA TRUE(), AKA ASSERT()):
test('Use OK()', t => {
const a = 1,
b = 2,
c = 'three';
eval(t.OK('a + b + c === "3three"'));
// when the condition fails we'll see:
// actual: {"a":1,"b":2,"c":"three"}
eval(t.OK('a + (b + c) === "12three"'));
});
test('Use ASSERT() with custom tester name', tester => {
// in this example we use a custom tester name "tester" instead of the default "t"
// it should be supplied as an option in the call (see below)
const a = 1,
b = 2,
c = 'three';
eval(tester.ASSERT('a + b + c === "3three"', {self: 'tester'}));
// lets log the evaluated code to inspect it:
console.log(tester.ASSERT('a + b + c === "3three"', {self: 'tester'}));
});