[Jest] Test JavaScript with Jest
JavaScript with test jest
2023-09-14 08:59:19 时间
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" },
相关文章
- Javascript中escape()、unescape()、encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()比较
- JavaScript经典代码【一】【javascript HTML控件获取值】
- [Javascript] Create an Async Generator and Loop Through Generated Promises with "For Await Of" Loops
- [Webpack] Analyze a Production JavaScript Bundle with webpack-bundle-analyzer
- [Javascript] Conditionally spread entries to a JavaScript object
- [Javascript] Delegate JavaScript (ES6) generator iteration control
- [Javascript] Combine Objects with Object.assign and Lodash merge
- [Javascript] Validate Data with the Every() Method
- [Javascript] Redirect the browser using JavaScript
- [Javascript] Linting JavaScript with ESLint
- [Javascript + rxjs] Using the map method with Observable
- Javascript 学习记录
- javascript 关于函数的返回值
- JavaScript规范
- [Javascript] Broadcaster + Operator + Listener pattern -- 10. Define a Function to Set Common Behaviors in Operators
- [Javascript] Simplify Creating Immutable Data Trees With Immer
- [Javascript] Maybe Functor
- [Javascript] Get Started with LeafletJS Mapping
- 给JavaScript的单个对象定义属性和属性的元数据
- JavaScript查找最长的公共前缀
- JavaScript基础语法(类型转换)
- JavaScript实现获取table中某一列的值