@cardano-sdk/util-dev

中文文档
0.25.38发布于 2026/09/25Apache-2.0TypeScriptESM + CJS近 30 天下载 3万

Utilities for tests in other packages

原始使用文档
文档目录3 节
  1. Tests
  2. TestLogger
  3. Tests development flow

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

版本与发布记录

@cardano-sdk/util-dev 共发布 208 个版本,首次发布于 2021/10/05;过去 12 个月发布了 6 个版本。

版本发布日期距今
0.25.38latest2026/09/251 个月前
0.25.372026/07/084 个月前
0.25.362026/07/044 个月前
0.25.342026/06/204 个月前
0.25.322026/04/136 个月前
0.25.312026/01/319 个月前

其他发布通道:test → 0.10.0-test.0,nightly → 0.9.1-nightly.1,patch → 0.17.2-patch.0。

关于 @cardano-sdk/util-dev 的常见问题

@cardano-sdk/util-dev 是什么?

Utilities for tests in other packages

如何安装 @cardano-sdk/util-dev?

在项目目录运行 npm install @cardano-sdk/util-dev,或使用 pnpm add / yarn add / bun add。

@cardano-sdk/util-dev 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cardano-sdk/util-dev 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cardano-sdk/util-dev 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cardano-sdk/util-dev 可以商用吗?

@cardano-sdk/util-dev 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@cardano-sdk/util-dev 的下载量有多少?

根据 npm 官方下载统计,@cardano-sdk/util-dev 在 2026/10/09 – 2026/11/07 这 30 天内被下载 29,981 次,日均约 999 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cardano-sdk/util-dev 还在维护吗?

最新版本 0.25.38 发布于 2026/09/25(1 个月前);至今共发布 208 个版本,其中过去 12 个月发布了 6 个;首次发布于 2021/10/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。