[Jest] Test JavaScript with Jest

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"
  },
原文地址:https://www.cnblogs.com/Answer1215/p/5836141.html