Skills2026年4月27日·1 分钟阅读

Chai — BDD/TDD Assertion Library for Node.js

Chai is a flexible assertion library for Node.js and browsers that supports expect, should, and assert styles.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Chai Overview
直接安装命令
npx -y tokrepo@latest install 2774d7ec-4278-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Chai is an assertion library for JavaScript that pairs with any test runner. It offers three distinct assertion styles: BDD-flavored expect and should chains, and a classic TDD assert interface. Chai's plugin architecture extends its vocabulary with domain-specific matchers for HTTP, promises, dates, and more.

What Chai Does

  • Provides expect(value).to.equal(...) chains for BDD-style assertions
  • Offers assert.strictEqual() for TDD-style assertions in the same library
  • Ships with deep equality, type checking, property inspection, and exception assertions
  • Supports chainable language helpers: .to, .be, .have, .that, .which
  • Extensible via plugins for sinon matchers, HTTP responses, promises, and dates

Architecture Overview

Chai builds assertion chains using a proxy-based object that records property accesses as flags (negation, deep equality, inclusion). When a terminal assertion method like .equal() is called, it reads the accumulated flags and evaluates the check. Failed assertions throw an AssertionError with a diff-friendly message. The plugin API lets third parties register new chainable properties and methods on the assertion prototype.

Self-Hosting & Configuration

  • Install via npm and import the style you prefer: expect, should, or assert
  • Use chai-as-promised for promise assertions: expect(promise).to.eventually.equal(...)
  • Pair with sinon-chai for mock verification assertions: expect(spy).to.have.been.calledOnce
  • Configure truncateThreshold to control how much of large objects appears in error messages
  • Works in Node.js and browsers via bundlers or the UMD build

Key Features

  • Three assertion styles in one library let teams pick the syntax they prefer
  • Chainable natural-language API produces readable test code
  • Deep equality comparison handles nested objects, arrays, and special types
  • Plugin ecosystem adds matchers for HTTP, dates, DOM elements, and more
  • Detailed error messages show expected vs. actual with diffs

Comparison with Similar Tools

  • Node.js assert — built-in but minimal; Chai adds richer matchers and better messages
  • Jest expect — bundled with Jest; Chai is runner-agnostic and works with Mocha, Vitest, or any framework
  • Unexpected — extensible assertion library; Chai has broader community adoption and more plugins
  • should.js — BDD-only; Chai offers three styles and is more actively maintained
  • power-assert — zero-API assertions with detailed diffs; Chai provides a richer vocabulary

FAQ

Q: Which assertion style should I use? A: expect is the most popular choice. It does not modify Object.prototype unlike should, and reads more naturally than assert.

Q: Does Chai work with TypeScript? A: Yes. Install @types/chai for type definitions, or use Chai 5+ which ships built-in TypeScript support.

Q: Can I use Chai with Jest? A: Technically yes, but Jest ships its own expect and mixing both can cause confusion. Chai is more commonly used with Mocha or Vitest.

Q: How do I add custom assertions? A: Use chai.use(plugin) where the plugin function receives chai and utils to register new methods on the assertion chain.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产