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实现单元测试的一些注意事项》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

创京东

创京东

李志刚 / 中信出版社 / 2015-5-1 / CNY 49.80

1998年,刘强东创业,在中关村经销光磁产品。2004年,因为非典,京东偶然之下转向线上销售。2014年,京东市值已超400亿美元,跻身全球前十大互联网公司之列。 这是一个听起来很传奇的创业故事,但只有当事人了解创业维艰。 刚转向电商时,传统企业前景光明,而电商看起来前途未卜,京东如何能毅然转型并坚持到底?资金匮乏的时候,京东靠什么说服投资人?在强大的对手面前,京东靠什么反超并一路领先......一起来看看 《创京东》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

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

在线压缩/解压 CSS 代码

在线进制转换器
在线进制转换器

各进制数互转换器