[Jest] Test JavaScript with Jest

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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"
  },

 

以上是关于[Jest] Test JavaScript with Jest的主要内容,如果未能解决你的问题,请参考以下文章

javascript nuxt.js项目中的Jest&vue-test-utils配置

Redux Saga:使用 redux-saga-test-plan 和 jest 在我的 saga 中测试纯 javascript 函数

Jest - 测试一个 ES6 类

sh [bash:jest related] #jest #unit_test相关#bash scipts

Vue-test-utils | Jest:如何处理依赖关系?

如何在此 Jest 测试中触发窗口加载事件?