Jest - Using test function to test the function

Note: Please check the prev blog to see the jest configuration.

 

 

calculator.js

const plus = (a, b) => {
  return a + b
}

const subtract = (a, b) => {
  return a - b
}


module.exports = {
  plus, subtract
}

 

calculator.test.js

 

// By default, Jest only supports standard JavaScript syntax. Once you want to import a function, you have to use 'require'
// const {plus, subtract} = require('./calculator.js')
// Add @babel/core @babel/preset-env to configure the .babelrc if you want to use import
import {plus, subtract} from "./calculator";

test("plus test: 1+4 === 5", ()=>{
  expect(plus(1,4)).toBe(5)
})

test("subtract test: 5 - 1 === 4", ()=>{
  expect(subtract(5,1)).toBe(4)
})

Then run the test script in the package.json

 

 

Result:

 

 

posted @ 2022-02-18 10:32  鑫仔Alan  阅读(25)  评论(0编辑  收藏  举报