zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Jest] Test JavaScript with Jest

JavaScript with test jest
2023-09-14 09:00:53 时间

Let's learn how to unit test your JavaScript with Jest, a JavaScript unit testing framework from Facebook. We'll install and optimize Jest for this project and see how quick and easy it is to get things going with Jest.

 

Install:

npm i jest-cli --save-dev

 

sum.js:

var R = require('ramda')

module.exports = sum;

function sum(ary){
    return R.sum(ary);
}

 

sum.test.js:

const sum = require('./sum')

test('adds 1 + 2 to equal 3', () => {
    expect(sum([1,2])).toBe(3)
})

 

Package.json:

Because jest simulate the broswer, so you are able to access 'window' object. But it is really not necessary for Node app.

So, you can config it in package.json:

  "jest": {
    "testEnvironment": "node"
  },