Configs2026年4月12日·1 分钟阅读

Jest — Delightful JavaScript Testing Framework

Jest is a delightful JavaScript testing framework with a focus on simplicity. Zero-config for most JS/TS projects, snapshot testing, mocking, code coverage, and parallel test execution. Created by Facebook and used to test React, Instagram, and many large codebases.

AI
AI Open Source · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

npm i -D jest @types/jest ts-jest
# Or for TypeScript projects
npx ts-jest config:init
// sum.ts
export function sum(a: number, b: number): number {
  return a + b;
}

// sum.test.ts
import { sum } from "./sum";

describe("sum", () => {
  test("adds 1 + 2 to equal 3", () => {
    expect(sum(1, 2)).toBe(3);
  });

  test("handles negative numbers", () => {
    expect(sum(-1, -2)).toBe(-3);
  });
});
npx jest                     # Run all tests
npx jest --watch             # Watch mode
npx jest --coverage          # With coverage report
介绍

Jest is a delightful JavaScript testing framework created by Facebook (now Meta). Zero-config for most React and TypeScript projects. Features include snapshot testing, built-in mocking, code coverage, parallel execution, and an intuitive API. Used to test React, Instagram, and many Meta properties.

What Jest Does

  • Zero-config — works out of the box for most JS/TS projects
  • Snapshot testing — capture and compare UI output
  • Mockingjest.fn(), jest.mock(), jest.spyOn()
  • Code coverage — built-in Istanbul coverage
  • Parallel execution — tests run in isolated workers
  • Watch mode — re-run changed tests
  • Matchers — rich assertion library (toBe, toEqual, toThrow, toMatchSnapshot)
  • Timer mocks — fake timers for setTimeout/setInterval
  • Async testing — async/await, promises, callbacks

Comparison

Runner Speed TS ESM Watch
Jest Medium Via ts-jest Experimental Yes
Vitest Fast Native Native Yes
Mocha Medium Via ts-node Yes Yes
Node test runner Fast Via tsx Yes Yes

常见问题 FAQ

Q: Jest vs Vitest? A: Vitest 更快(Vite 驱动)、ESM 原生、TS 无需 ts-jest。新 Vite 项目推荐 Vitest。Jest 生态更大、存量项目更多。

Q: TypeScript 怎么配? A: 用 ts-jest 转换器或 @swc/jest(更快)。配置 jest.config.tstransform 字段。

来源与致谢 Sources

讨论

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

相关资产