@alex_neo/jest-expect-message

中文文档
1.0.5发布于 2021/06/22MITCommonJS零依赖近 30 天下载 6.2万

Add custom message to Jest expects

原始使用文档
文档目录9 节
  1. Problem
  2. Solution
  3. Installation
  4. Setup
  5. Jest v24+
  6. Jest v23-
  7. Usage
  8. Contributors
  9. LICENSE

jest-expect-message

🃏🗯

Add custom message to Jest expects


Build Status Code Coverage version downloads MIT License PRs Welcome Roadmap Examples

Problem

In many testing libraries it is possible to supply a custom message for a given expectation, this is currently not possible in Jest.

For example:

test('returns 2 when adding 1 and 1', () => {
  expect(1 + 1, 'Woah this should be 2!').toBe(3);
});

This will throw the following error in Jest:

Expect takes at most one argument.

Solution

jest-expect-message allows you to call expect with a second argument of a String message.

For example the same test as above:

test('returns 2 when adding 1 and 1', () => {
  expect(1 + 1, 'Woah this should be 2!').toBe(3);
});

With jest-expect-message this will fail with your custom error message:

  ● returns 2 when adding 1 and 1

    Custom message:
      Woah this should be 2!

    expect(received).toBe(expected) // Object.is equality

    Expected: 3
    Received: 2

Installation

With npm:

npm install --save-dev jest-expect-message

With yarn:

yarn add -D jest-expect-message

Setup

Add jest-expect-message to your Jest setupFilesAfterEnv configuration. See for help

Jest v24+

"jest": {
  "setupFilesAfterEnv": ["jest-expect-message"]
}

Jest v23-

"jest": {
  "setupTestFrameworkScriptFile": "jest-expect-message"
}

Usage

  • expect(actual, message)
    • actual: The value you would normally pass into an expect to assert against with a given matcher.
    • message: String, the custom message you want to be printed should the expect fail.
test('returns 2 when adding 1 and 1', () => {
  expect(1 + 1, 'Woah this should be 2!').toBe(3);
});

Contributors


Matt Phillips

💻 📖 💡 🤔 🚇 ⚠️ 🔧

Alex Neo

💻

LICENSE

MIT

版本与发布记录

@alex_neo/jest-expect-message 共发布 2 个版本,首次发布于 2021/06/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.5latest2021/06/225 年前
1.0.42021/06/225 年前

关于 @alex_neo/jest-expect-message 的常见问题

@alex_neo/jest-expect-message 是什么?

Add custom message to Jest expects

如何安装 @alex_neo/jest-expect-message?

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

@alex_neo/jest-expect-message 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/alex_neo__jest-expect-message 是否存在。

@alex_neo/jest-expect-message 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@alex_neo/jest-expect-message 可以商用吗?

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

@alex_neo/jest-expect-message 的下载量有多少?

根据 npm 官方下载统计,@alex_neo/jest-expect-message 在 2026/10/06 – 2026/11/04 这 30 天内被下载 62,013 次,日均约 2,067 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alex_neo/jest-expect-message 还在维护吗?

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