@cardano-sdk/util-dev
v0.24.3
Published
Utilities for tests in other packages
Downloads
11,785
Readme
Cardano JS SDK | util-dev
Utilities for tests in other packages
Tests
See code coverage report.
TestLogger
A unit tests dedicated logger.
import { createLogger, logger } from '@cardano-sdk/util-dev';
import { deepStrictEqual } from 'assert';
logger.debug('Some debug data');
logger.error('Some error data');
// etc...
const newLogger = createLogger({ record: true });
newLogger.info('another message');
newLogger.debug({ test: 42 }, 'some message');
newLogger.error('error message', new Error('example error'));
deepStrictEqual(newLogger.messages, [
{ level: 'info', message: ['another message'] },
{ level: 'debug', message: [{ test: 42 }, 'some message'] },
{ level: 'error', message: ['error message', new Error('example error')] }
]);
This logger is expressly designed to be used in unit tests: it keeps the unit tests output as clean as possible (logging only messages with fatal
level), but, when required, it offers a quick and easy way to increase the verbosity of the tests without the need to change any file.
It also offer the option to record all the logged values to perform checks on what is logged by the tested program.
Tests development flow
While developing / maintaining / reviewing unit tests we often run the unit test command from our shell:
yarn test
As long as all the tests pass, we don't even need TestLogger
.
But sometimes it may happen that some tests fail... sometimes...
In order to investigate the reason of the failure of a test, may be useful to access the logs.
If the test was written using TestLogger
, the latter allows us to access and / or customize the logs through simple environment variables.
To increase the log level, just use the TL_LEVEL
environment variable:
TL_LEVEL=info yarn test
By default, TestLogger
uses util.inspect and its options
object can be customized with following environment variables:
TL_ARRAY
maps tomaxArrayLength
. Defaults to100
.0
maps toInfinity
.TL_BREAK
maps tobreakLength
. Defaults to tty width on a tty or90
otherwise.0
maps toInfinity
.TL_COLOR
maps tocolors
. Defaults totrue
on a ttyfalse
otherwise.TL_COMPACT
maps tocompact
. Defaults to3
.0
maps tofalse
.TL_DEPTH
maps todepth
. Defaults to2
.0
maps toInfinity
.TL_HIDDEN
maps toshowHidden
. Defaults tofalse
.TL_PROXY
maps toshowProxy
. Defaults tofalse
.TL_STRING
maps tomaxStringLength
. Defaults to1000
(not10000
asutil.inspect
does).0
maps toInfinity
.
so if (for example) we need to log full nested objects and Proxy
s we could run our test command with:
TL_PROXY=true TL_DEPTH=0 TL_LEVEL=info yarn test
Even if util.inspect
can be deeply customized (letting us to customize our log as well), it has the drawback that its output can't be cut and pasted on a source file or to perform a POST etc.
If we need to cut paste something from our log we can instruct TestLogger
to use JSONBig.stringify
instead; this will obviously make TestLogger
to ignore environment variables which customize util.inspect
.
TL_LEVEL=info TL_JSON=true yarn test