Repository navigation
Open
Description
如果你的 node 没有写单元测试,那可以跳过本文了。
1. 缘起
在我们日常的单元测试中,常用的断言库有:
user.should.have.property('name', 'tz');
user.enabled.should.ok;
expect(5).to.be.a('number');
expect(window).not.to.be.an(Image);存在什么问题呢?
- 复杂的 API,每次使用时,都需要去翻文档,用自然语言描述测试真的好么?
- 经常怀疑人生,
user.enabled.should.ok;这句到底有没有执行? 还是只是取值? - 一脸懵逼,反馈信息不足,往往我们还需要加
log再跑一次,如果在 ci 上看日志就懵逼了。
require('should');
const expect = require('expect.js');
const assert = require('assert');
describe('test/showcase.test.js', () => {
const arr = [ 1, 2, 3 ];
it('should.js', () => {
arr[1].should.eql(10);
});
it('expect.js', () => {
expect(arr[1]).to.eql(10);
});
it('assert', () => {
// 用原生的话, 得到的提示更是一脸懵逼
assert(arr[1] === 10);
});
});
// output:
1) test/showcase.test.js should.js:
AssertionError: expected 2 to equal 10
...
2) test/showcase.test.js expect.js:
Error: expected 2 to sort of equal 10
...
3) test/showcase.test.js assert:
AssertionError: false == true
...2. 曙光
在 egg 的开发中, 我们发现了不一样的它:
Power Assert in JavaScript.
Provides descriptive assertion messages through standard assert interface.
No API is the best API.
简单的说,它的优点是:
- 没有 API 就是最好的 API,不需要任何记忆,只需
assert即可。 - 强大的错误信息反馈
- 强大的错误信息反馈
- 强大的错误信息反馈
const assert = require('power-assert');
describe('test/showcase.test.js', () => {
const arr = [ 1, 2, 3 ];
it('power-assert', () => {
assert(arr[1] === 10);
});
});
// output:
4) test/showcase.test.js power-assert:
AssertionError: # test/showcase.test.js:6
assert(arr[1] === 10)
| | |
| 2 false
[1,2,3]
[number] 10
=> 10
[number] arr[1]
=> 23. 使用
在线尝试:https://azu.github.io/power-assert-demo/
安装依赖:
$ npm i mocha power-assert intelli-espower-loader --save-dev配置 npm scripts:
{
"scripts": {
"test": "mocha -r intelli-espower-loader test/**/*.test.js",
},
"devDependencies": {
"power-assert": "^1.4.2",
"intelli-espower-loader": "^1.0.1",
}
}编写测试:
// 简单的 require, 使用者无感知
// 下面的代码没写错, 无需 `require('power-assert')`, loader 会自动替换
const assert = require('assert');
describe('test/showcase.test.js', () => {
const arr = [ 1, 2, 3 ];
it('power-assert', () => {
// 完全兼容 node 原生的 assert API, 直接自由使用
assert(arr[1] === 10);
});
});执行测试:
$ npm test4. 其他
- mocha 需要引入
intelli-espower-loader,主要是把代码转译,参见作者的 slide . - 转译之后, 你甚至完全无感知,
require('assert')都不需要改. 因为转译,所以不能用原生的assert了, 否则会遇到如下错误- TypeError: assert._capt is not a function
简单的说, 把原来测试代码里面的require('assert')改为require('power-assert')即可- 花掉时间重构下旧的代码,带来的是全新的体验。
- 该问题已经被解决了: upgrade espower-source to 2.0.0 power-assert-js/espower-loader#5
5. 补充
- 如果你嫌弃配置麻烦,可以直接用我们的 egg-bin
- 推荐阅读: https://eggjs.org/zh-cn/core/unittest.html
Reacted by Shawn, Haoliang Gao, Yiyu He, Sebastian Blade, 仙森, jianleiHuang, 宗羽(zōng yǔ), hacke2, MK (fengmk2), The Rock and 26 more
Reacted by xgqfrms
You can’t perform that action at this time.
Activity
[-]No API is the best API - 抛弃 should.js 吧[/-][+]No API is the best API - 抛弃 should/expect/chai 吧[/+]fengmk2 commentedon Dec 6, 2016
报错体验实在太好,有时候都忍不住写错代码来体验一把。
Clarence-pan commentedon Dec 7, 2016
牛x,试了一把,果然厉害
DavidCai1111 commentedon Dec 7, 2016
这报错体验好到泪目~👍
mlyknown commentedon Dec 8, 2016
666
joyeecheung commentedon Dec 15, 2016
从其他库转过来的要小心 deepEqual 和 deepStrictEqual 的区别……(好多库的 deep equal 其实是 deepStrictEqual) nodejs/node#10258
atian25 commentedon Dec 15, 2016
补充下, 无需
require('power-assert'), loader 会自动替换也就是说, 只需要
require('assert');, 使用者完全无感知atian25 commentedon Dec 15, 2016
同时, 如果你嫌麻烦的话, 直接用我们的 egg-bin
直接在 scripts 里面加入即可:
{ "scripts": { "test": "egg-bin test", "cov": "egg-bin cov" }, "devDependencies": { "egg-bin": "^1.9.0" } }kazupon commentedon Dec 15, 2016
FYI:
official released
unassert.js
https://github.com/unassert-js
Tweet of creator (sorry, japanese)
https://twitter.com/t_wada/status/809242080027414528
oychao commentedon Jan 8, 2017
有没有gulp的用法示例~
atian25 commentedon Jan 8, 2017
@oychao https://github.com/power-assert-js/gulp-espower
oychao commentedon Jan 8, 2017
已经实现了,刚找到这一篇博客,作为一个日常日本IT黑,看到一个如此优秀的JS库的作者居然是个日本人,http://efcl.info/2014/0406/res3809/
oychao commentedon Jan 8, 2017
太舒服,专门写了半个小时错误,就为了看报错结果~
2 remaining items
atian25 commentedon Mar 23, 2017
推荐阅读: https://eggjs.org/zh-cn/core/unittest.html
mosaic101 commentedon Mar 24, 2017
赞!好东西,必须用起来
huangjihua commentedon Mar 28, 2017
真是好东西,赞
cncoder commentedon Mar 28, 2017
junit有就好了Hhhh
lexign commentedon Apr 20, 2017
什么时候支持Async/Await呢?
atian25 commentedon Apr 20, 2017
@Chalin-Shi 支持的啊
lexign commentedon Apr 20, 2017
@atian25 我上午测试了下,报错,我以为不支持呢。难道是因为不支持外部import的文件吗?还是必须要用node version > 7.6.0,或者babel呢。
atian25 commentedon Apr 20, 2017
power-assert没有关系lexign commentedon Apr 20, 2017
@atian25 谢谢你。我本地node用的是6.10版本,应该是这个原因了,然后我准备测试时用7.6+,部署是用6.10,不知道这样好不好。。。
lexign commentedon Apr 20, 2017
目前准备报koa2的项目,转到eggjs,感觉文档好全,很不错,貌似next版本才支持Async/Await是吗?
atian25 commentedon Apr 20, 2017
crycime commentedon Apr 23, 2019
babel配合使用不支持怎么解决哈
atian25 commentedon Apr 23, 2019
https://github.com/power-assert-js/power-assert
看官方 README,有 babel 插件
crycime commentedon Apr 23, 2019
解决了
yumao233 commentedon Nov 2, 2019
确实,赞同 "没有API就是最好的API" 这句话。