mocha配合babel7实现单元测试的一些注意事项

栏目: 编程工具 · 发布时间: 6年前

内容简介:安装在自己的项目上最终:最终:

安装在自己的项目上

yarn add mocha chai -D
// 或
npm i --save-dev mocha chai
复制代码

最终:

"devDependencies": {
   "chai": "^4.2.0",
   "mocha": "^6.0.2"
}
复制代码

babel7的安装

yarn add @babel/core @babel/cli @babel/preset-env @babel/register -D
// 或
npm i --save-dev @babel/core @babel/cli @babel/preset-env @babel/register
复制代码

最终:

"devDependencies": {
   "@babel/cli": "^7.2.3",
   "@babel/core": "^7.4.0",
   "@babel/preset-env": "^7.4.2",
   "@babel/register": "^7.4.0",
   "chai": "^4.2.0",
   "mocha": "^6.0.2"
}
复制代码

增加babel.config.js文件

const presets = [
  [
    "@babel/env"
  ]
]
module.exports = { presets }
复制代码

添加test命令脚本

"scripts": {
   "test": "./node_modules/.bin/mocha --require @babel/register test/*/*.spec.js"
}
复制代码

1、其中的重点在于--require @babel/register这个命令,在目前找到的大多数教程中用的是

// babel7
--compilers js:@babel/register
// babel7之前
--compilers js:babel-core/register
复制代码

但实际上--compilers这个方法已经被废弃了,具体可以查看wiki: compilers-deprecation

2、test/*/*.spec.js指向的是测试文件位置

实现测试方法

util目录下的date.js

/**
 * @description 闰年判断
 * @date 2019-03-26
 * @param {*} year
 * @returns {boolean}
 */
export function isLeapYear(year) {
  if (!year) return
  return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)
}

/**
 * @description 获取该月有多少天
 * @date 2019-03-26
 * @param {(string | number)} date
 * @returns {number}
 */
export function getDaysOfMonth(date) {
  let d
  if (typeof date === "string" || typeof date === "number") {
    d = new Date(date)
  } else d = date
  const month = d.getMonth() + 1
  const year = d.getFullYear()
  if (month === 2) return isLeapYear(year) ? 29 : 28
  if ([1, 3, 5, 7, 8, 10, 12].includes(month)) return 31
  else return 30
}
复制代码

test/unit/date.spec.js

import { isLeapYear, getDaysOfMonth } from '../../util/date'
import { expect } from 'chai'

describe('isLeapYear', () => {
  it('2020年是闰年', () => {
    expect(isLeapYear(2020)).to.be.true
  })
  it('今年不是闰年', () => {
    expect(isLeapYear(2019)).to.be.false
  })
  it('2000年是闰年', () => {
    expect(isLeapYear(2000)).to.be.true
  })
})

describe('getDaysOfMonth', () => {
  it('本月有31天', () => {
    expect(getDaysOfMonth(new Date())).to.equal(31)
  })
  it('1月有31天', () => {
    expect(getDaysOfMonth('2019-01-12')).to.equal(31)
  })
  it('2月有28天', () => {
    expect(getDaysOfMonth('2019-02-12')).to.equal(28)
  })
})
复制代码

执行yarn test或npm run test,最终结果如下:

mocha配合babel7实现单元测试的一些注意事项

生成测试覆盖率报告

如果有需要的话我们也可以使用 mochawesome 生成一个代码覆盖率报告,其展示形式包括页面展示、json文件等

安装:

yarn add mochawesome -D
// 或
npm install --save-dev mochawesome
复制代码

可以通过配置来指定报告生成的位置及文件名,具体操作为在test脚本命令中加入

// reportDir 指报告生成的文件夹
// reportFilename 报告名称
--reporter mochawesome --reporter-options reportDir=test/report,reportFilename=date

// 最终的test脚本
"scripts": {
    "test": "./node_modules/.bin/mocha --require @babel/register test/*/*.spec.js --reporter mochawesome --reporter-options reportDir=test/report,reportFilename=date"
}
复制代码

再次运行脚本可以看到

mocha配合babel7实现单元测试的一些注意事项

测试报告已经写入了指定位置

页面展示如下

mocha配合babel7实现单元测试的一些注意事项

以上所述就是小编给大家介绍的《mocha配合babel7实现单元测试的一些注意事项》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

产品经理修炼之道

产品经理修炼之道

费杰 / 机械工业出版社华章公司 / 2012-7-30 / 59.00元

本书由资深产品经理、中国最大的产品经理沙龙Pmcaff创始人费杰亲自执笔,微软、腾讯、百度、新浪、搜狐、奇虎、阿里云、Evernote等国内外20余家大型互联网企业资深产品经理和技术专家联袂推荐。用系统化的方法论和丰富的实战案例解读了优秀产品经理所必须修炼的产品规划能力、产品设计能力、产品执行能力,以及思考、分析和解决问题的能力和方法,旨在为互联网产品经理打造核心竞争力提供实践指导。 全书一......一起来看看 《产品经理修炼之道》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具