npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cardano-sdk/util-dev

v0.23.12

Published

Utilities for tests in other packages

Downloads

9,473

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 to maxArrayLength. Defaults to 100. 0 maps to Infinity.
  • TL_BREAK maps to breakLength. Defaults to tty width on a tty or 90 otherwise. 0 maps to Infinity.
  • TL_COLOR maps to colors. Defaults to true on a tty false otherwise.
  • TL_COMPACT maps to compact. Defaults to 3. 0 maps to false.
  • TL_DEPTH maps to depth. Defaults to 2. 0 maps to Infinity.
  • TL_HIDDEN maps to showHidden. Defaults to false.
  • TL_PROXY maps to showProxy. Defaults to false.
  • TL_STRING maps to maxStringLength. Defaults to 1000 (not 10000 as util.inspect does). 0 maps to Infinity.

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 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