jest-util

中文文档
30.5.1发布于 2026/09/01MITTypeScriptESM + CJS近 30 天下载 4.7亿

This packages is a collection of utilities and helper functions

原始使用文档
文档目录21 节
  1. ErrorWithStack
  2. clearLine
  3. convertDescriptorToString
  4. createDirectory
  5. deepCyclicCopy
  6. formatTime
  7. globsToMatcher
  8. installCommonGlobals
  9. interopRequireDefault
  10. invariant
  11. isInteractive
  12. isNonNullable
  13. isPromise
  14. pluralize
  15. preRunMessage
  16. replacePathSepForGlob
  17. requireOrImportModule
  18. setGlobal
  19. specialChars
  20. TestPathPatterns
  21. tryRealpath

@jest/utils

This packages is a collection of utilities and helper functions

ErrorWithStack

This custom error class can be useful when you need to capture the stack trace of an error and provide additional context to the error message. By default, JavaScript errors only capture the stack trace when they are thrown, but this class allows you to capture the stack trace at any point in your code by calling its constructor.

clearLine

It ensures that the clearing operation is only performed when running in a terminal environment, and not when the output is redirected to a file or another non-terminal destination.

convertDescriptorToString

It defines a function named convertDescriptorToString that takes a descriptor as input and converts it to a string based on its type. It handles various types such as functions, numbers, strings, and undefined values. If the input doesn't match any of these types, it throws an error with a descriptive message.

createDirectory

It creates new directory and also allows creation of nested directories.

deepCyclicCopy

The deepCyclicCopy function provides deep copying of JavaScript objects and arrays, including handling circular references. It offers optional customization through a DeepCyclicCopyOptions parameter, allowing users to blacklist properties and preserve object prototypes. The function returns a completely independent deep copy of the input data structure.

formatTime

This function is useful for formatting time values with appropriate SI unit prefixes for readability. It expresses time in various units (e.g., milliseconds, microseconds, nanoseconds) while ensuring the formatting is consistent and human-readable.

globsToMatcher

The code efficiently converts a list of glob patterns into a reusable function for matching paths against those patterns, considering negated patterns and optimizing for performance.

installCommonGlobals

Sets up various global variables and functions needed by the Jest testing framework. It ensures that these globals are properly set up for testing scenarios while maintaining compatibility with the environment's global object.

interopRequireDefault

Provides a way to ensure compatibility between ES modules and CommonJS modules by handling the default export behavior appropriately.

invariant

It is a utility used for asserting that a given condition is true. It's often used as a debugging aid during development to catch situations where an expected condition is not met.

isInteractive

Checks whether the current environment is suitable for interactive terminal interactions.

isNonNullable

Used to narrow down the type of a variable within a TypeScript code block, ensuring that it is safe to assume that the value is non-nullable. This can help avoid runtime errors related to null or undefined values.

isPromise

It helps in order to determine whether a given value conforms to the structure of a Promise-like object, which typically has a then method. This can be useful when working with asynchronous code to ensure dealing with promises correctly.

pluralize

This function is used to easily generate grammatically correct phrases in text output that depend on the count of items. It ensures that the word is correctly pluralized when needed.

preRunMessage

These functions are intended for use in interactive command-line tools or scripts which provide informative messages to the user while ensuring a clean and responsive interface.

replacePathSepForGlob

The function takes a string path as input and replaces backslashes ('\') with forward slashes ('/') in the path. Used to normalize file paths to be compatible with glob patterns, ensuring consistency in path representation for different operating systems.

requireOrImportModule

This function provides a unified way to load modules regardless of whether they use CommonJS or ESM syntax. It ensures that the default export is applied consistently when needed, allowing users to work with modules that might use different module systems.

setGlobal

Used to set properties with specified values within a global object. It is designed to work in both browser-like and Node.js environments by accepting different types of global objects as input.

specialChars

It defines constants and conditional values for handling platform-specific behaviors in a terminal environment. It determines if the current platform is Windows ('win32') and sets up constants for various symbols and terminal screen clearing escape sequences accordingly, ensuring proper display and behavior on both Windows and non-Windows operating systems.

TestPathPatterns

This class takes test patterns and provides the API for deciding if a test matches any of the patterns.

tryRealpath

Used to resolve the real path of a given path, but if the path doesn't exist or is a directory, it doesn't throw an error and returns the original path string. This can be useful for gracefully handling path resolution in scenarios where some paths might not exist or might be directories.

版本与发布记录

jest-util 共发布 277 个版本,首次发布于 2016/02/17;过去 12 个月发布了 5 个版本。

版本发布日期距今
30.5.1latest2026/09/012 个月前
30.5.02026/08/282 个月前
30.4.12026/05/085 个月前
30.4.02026/05/085 个月前
30.3.02026/03/107 个月前
30.2.02025/09/281.1 年前

其他发布通道:next → 30.0.0-rc.1。

关于 jest-util 的常见问题

jest-util 是什么?

This packages is a collection of utilities and helper functions

如何安装 jest-util?

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

jest-util 支持 TypeScript 吗?

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

jest-util 支持 ESM 还是 CommonJS?

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

jest-util 需要什么版本的 Node.js?

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

jest-util 可以商用吗?

jest-util 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

jest-util 的下载量有多少?

根据 npm 官方下载统计,jest-util 在 2026/10/05 – 2026/11/03 这 30 天内被下载 465,469,569 次,日均约 15,515,652 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

jest-util 还在维护吗?

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