@cardano-sdk/util-dev v0.25.18
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 testAs 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 testBy default, TestLogger uses util.inspect and its options object can be customized with following environment variables:
TL_ARRAYmaps tomaxArrayLength. Defaults to100.0maps toInfinity.TL_BREAKmaps tobreakLength. Defaults to tty width on a tty or90otherwise.0maps toInfinity.TL_COLORmaps tocolors. Defaults totrueon a ttyfalseotherwise.TL_COMPACTmaps tocompact. Defaults to3.0maps tofalse.TL_DEPTHmaps todepth. Defaults to2.0maps toInfinity.TL_HIDDENmaps toshowHidden. Defaults tofalse.TL_PROXYmaps toshowProxy. Defaults tofalse.TL_STRINGmaps tomaxStringLength. Defaults to1000(not10000asutil.inspectdoes).0maps toInfinity.
so if (for example) we need to log full nested objects and Proxys we could run our test command with:
TL_PROXY=true TL_DEPTH=0 TL_LEVEL=info yarn testEven 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 test5 months ago
7 months ago
6 months ago
5 months ago
8 months ago
7 months ago
7 months ago
7 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
1 year ago
12 months ago
12 months ago
11 months ago
12 months ago
12 months ago
10 months ago
10 months ago
11 months ago
11 months ago
11 months ago
11 months ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago