What Jest Does
- Zero-config — works out of the box for most JS/TS projects
- Snapshot testing — capture and compare UI output
- Mocking —
jest.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.ts 的 transform 字段。
来源与致谢 Sources
- Docs: https://jestjs.io
- GitHub: https://github.com/jestjs/jest
- License: MIT