前端学习之路jest——小小demo

demo.js部分——需要测试的文件和方法

function jestdemo1fn(num){
    return num>=5?'休息':'工作'
}
function jestdemo2fn(num){
    return num>=1?'onetwothree':'123'
}
module.exports={
    jestdemo1fn, jestdemo2fn
}

demo.test.js部分——自动化测试文件

const demojs = require('./demo');
const {jestdemo1fn,jestdemo2fn} = demojs
test('工作状态',()=>{
    expect(jestdemo1fn(2)).toBe('工作')
})

test('中英文2',()=>{
    expect(jestdemo2fn(2)).toBe('onetwothree')
})

test的第一个参数 是这个测试的描述 后面跟着的就是一个简单的方法名 传递的参数 toBe后是得到的结果
需要注意的点是expect 不是export 别写错了

修改你的package的部分

将scripts中 test 的值 修改成jest

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

在控制台运行

npm run test

在这里插入图片描述
如果出错和测试结果不符合则是下面这种

const demojs = require('./demo');
const {jestdemo1fn,jestdemo2fn} = demojs
test('工作状态',()=>{
    expect(jestdemo1fn(2)).toBe('工作')
})

test('中英文2',()=>{
    expect(jestdemo2fn(2)).toBe('123')
})

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值