初探Jest

139 阅读1分钟

1.新建demo文件夹,运行yarn add jest

然后在package.json里添加:

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

2.我们需要新建两个文件:Math.jsMath.test.js

//Math.js
function Math(){
    this.add = function(x,y){
        return x+y
    }
}

module.exports = {Math}
//Math.test.js
let {Math} = require('./Math')

test("测试通过惹~",()=>{
    let math = new Math()
    expect(math.add(3,5)).toBe(8)
})

3.运行测试yarn test Math.test.js(运行yarn test默认测试所有的.test.js文件)