diff --git a/docs/jest.html b/docs/jest.html index f49fc414..0b8e2d9d 100644 --- a/docs/jest.html +++ b/docs/jest.html @@ -37,7 +37,9 @@

Jest 备忘清单

Jest 是一款优雅、简洁的 JavaScript 测试框架。

入门

-

介绍

+ +

介绍

+

Jest 是一款优雅、简洁的 JavaScript 测试框架。

  • 无需配置 大多数 JS 项目中即装即用,无需配置
  • @@ -46,8 +48,37 @@
  • 快照 轻松编写持续追踪大型对象的测试,并在测试旁或代码内显示实时快照。
  • 代码覆盖 无需其他操作,您仅需添加 --coverage 参数来生成代码覆盖率报告。
-

测试结构

+

快速开始

+ +
npm install --save-dev jest
+
+

Add to package.json

+
"scripts": {
+  "test": "jest"
+}
+
+

运行你的测试

+
npm test -- --watch
+
+

查看: Getting started

+

编写测试

+
describe('My work', () => {
+  test('works', () => {
+    expect(2).toEqual(2)
+  })
+})
+
+

BDD 语法

+ +
describe('My work', () => {
+  it('works', () => { // `it`是`test`的别名
+    ···
+  })
+})
+
+

测试结构

+
describe('makePoniesPink', () => {
   beforeAll(() => {
     /* 在所有测试之前运行 */
@@ -67,6 +98,58 @@
   })
 })
 
+

聚焦测试

+
describe.only(···)
+
+it.only(···)
+// 别名: fit()
+
+

查看: test.only

+

可选参数

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
FlagDescription
--coverage查看测试覆盖率摘要
--detectOpenHandles查看未关闭端口的摘要
--runInBand一个接一个地运行所有测试
--bail,-b失败跳出测试
+

更多参数查看 Jest CLI Options

+

跳过测试

+
describe.skip(···)
+
+it.skip(···)
+// 别名: xit()
+
+

查看: test.skip

+

基本测试用例

+ +
expect(value).not.toBe(value)
+  .toEqual(value)
+  .toBeTruthy()
+// Errors 测试
+expect(value).toThrow(error)
+  .toThrowErrorMatchingSnapshot()
+

匹配器

基本匹配器

expect(42).toBe(42)    // 严格相等 (===)
@@ -517,7 +600,7 @@
 })
 

describe.each() 文档、test.each() 文档

-

跳过测试

+

跳过测试

不要运行这些测试

describe.skip('makePoniesPink'...