Jest测试框架-Jest基本环境搭建

Jest测试框架需要的工具

Node.js
NPM
VScode

Jest测试框架安装

yarn 安装 Jest

yarn add --dev jest

因为在此系统上禁止运行脚本,解决方法

yarn : 无法加载文件
C:\Users\Tommy\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本。 有关详细信息,请参阅
https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Polic
ies。

开始>pwoershell>以管理身份打开,设置允许执行脚本策略

set-ExecutionPolicy RemoteSigned
根据提示输入:Y

查看当前策略设置情况

get-ExecutionPolicy

npm 安装 Jest

npm install --save-dev jest

Jest 测试框架配置package.json

将package.json里面的scripts,设置jest框架,保存即可

"scripts": {
    
    
    "test": "jest" 
  },

Jest快速测试Demo

一般我们要区分好,存放业务的.js和存在用例.test.js

建立一个Jest的业务文件web.js

// 存放业务文件

function web1(money){
    
    
    return money>=200?"vip":"menber"
}

function web2(money){
    
    
    return money>=1000?"svip":"glodvip"
}

module.exports={
    
    
    web1,web2
}

建立一个Jest执行测试文件web.test.js

就是多了一个test的扩张名

// 存放测试用例

const web= require("./web");
const {
    
    web1,web2}=web

test("200vip",()=>{
    
    
    expect(web1(100)).toBe("vip")
})

test("1000vip",()=>{
    
    
    expect(web2(2000)).toBe("svip")
})

执行Jest 测试

yarn test
npm run test

命令窗口中查看Jest测试结果

yarn run v1.22.10
$ jest
 FAIL  ./web.test.js
  × 200vip (5 ms)
  √ 1000vip

  ● 200vip

    expect(received).toBe(expected) // Object.is equality

    Expected: "vip"
    Received: "menber"

      3 | 
      4 | test("200vip",()=>{
    
    
    > 5 |     expect(web1(100)).toBe("vip")
        |                       ^
      6 | })
      7 | 
      8 | test("1000vip",()=>{
    
    

      at Object.<anonymous> (web.test.js:5:23)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        1.634 s
Ran all test suites.
error Command failed with exit code 1.

Jest测试框架初始化配置

执行jest测试框架初始化时,确保他在项目的根目录

npx jest --init
√ Would you like to use Typescript for the configuration file? ... no
√ Choose the test environment that will be used for testing » jsdom (browser-like)
√ Do you want Jest to add coverage reports? ... yes
√ Which provider should be used to instrument code for coverage? » babel
√ Automatically clear mock calls and instances between every test? ... yes

生成代码覆盖率

npx jest --coverage

快速配置和使用jest --coverage

将package.json里面的scripts,添加项

扫描二维码关注公众号,回复: 12568352 查看本文章
"scripts": {
    
    
    "test": "jest" ,
    "coverage":"jest --coverage"
  },
npm run coverage
yarn coverage

用例执行覆盖率报告

 FAIL  ./web.test.js
  × 200vip (5 ms)
  √ 1000vip

  ● 200vip

    expect(received).toBe(expected) // Object.is equality

    Expected: "vip"
    Received: "menber"

      3 | 
      4 | test("200vip",()=>{
    
    
    > 5 |     expect(web1(100)).toBe("vip")
        |                       ^
      6 | })
      7 | 
      8 | test("1000vip",()=>{
    
    

      at Object.<anonymous> (web.test.js:5:23)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |       50 |     100 |     100 | 
 web.js   |     100 |       50 |     100 |     100 | 4-8
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        1.691 s
Ran all test suites.

猜你喜欢

转载自blog.csdn.net/qq_30864373/article/details/111659779