jest体验

120 阅读1分钟

一个成熟的库,必须要测试,所以最近学习了一波jest。

1. 安装

npm i jest -D

2. jest配置项

npx jest --init 根据询问选择配置项,产生一个jest.config.js文件

3. 单元测试覆盖率

"test": "jest --watch" "coverage": "jest --coverage"

4. 支持es6模块化

需要安装 @babel/core@7.4.5 @babel/preset-env@7.4.5 这两个依赖 并在项目根目录下新建.babelrc文件,

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

5. 实例

import { fn1 } from "./index.js"

describe("form组件",()=> {
  it("数组包含",()=>{
    expect([1,13,2]).toContain(1);
  });
  it("函数",()=>{
    expect(fn1(1)).toBe(20);
  });
  it(">=",()=>{
    expect(10).toBeGreaterThan(1);
  });
})

# 完!