Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No API is the best API - 抛弃 should/expect/chai 吧 #16

Open
atian25 opened this issue Dec 6, 2016 · 29 comments
Open

No API is the best API - 抛弃 should/expect/chai 吧 #16

atian25 opened this issue Dec 6, 2016 · 29 comments

Comments

@atian25
Copy link
Owner

@atian25 atian25 commented Dec 6, 2016

如果你的 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.

https://github.com/power-assert-js/power-assert

简单的说,它的优点是:

  • 没有 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]
  => 2

image

3. 使用

在线尝试: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 test

4. 其他

  • mocha 需要引入 intelli-espower-loader,主要是把代码转译,参见作者的 slide .
  • 转译之后, 你甚至完全无感知, require('assert') 都不需要改.
  • 因为转译,所以不能用原生的 assert 了, 否则会遇到如下错误

5. 补充

@atian25 atian25 changed the title No API is the best API - 抛弃 should.js 吧 No API is the best API - 抛弃 should/expect/chai 吧 Dec 6, 2016
@fengmk2
Copy link

@fengmk2 fengmk2 commented Dec 6, 2016

报错体验实在太好,有时候都忍不住写错代码来体验一把。

@Clarence-pan
Copy link

@Clarence-pan Clarence-pan commented Dec 7, 2016

牛x,试了一把,果然厉害

@DavidCai1993
Copy link

@DavidCai1993 DavidCai1993 commented Dec 7, 2016

这报错体验好到泪目~👍

@mlyknown
Copy link

@mlyknown mlyknown commented Dec 8, 2016

666

@joyeecheung
Copy link

@joyeecheung joyeecheung commented Dec 15, 2016

从其他库转过来的要小心 deepEqual 和 deepStrictEqual 的区别……(好多库的 deep equal 其实是 deepStrictEqual) nodejs/node#10258

@atian25
Copy link
Owner Author

@atian25 atian25 commented Dec 15, 2016

补充下, 无需 require('power-assert'), loader 会自动替换

也就是说, 只需要 require('assert'); , 使用者完全无感知

@atian25
Copy link
Owner Author

@atian25 atian25 commented Dec 15, 2016

同时, 如果你嫌麻烦的话, 直接用我们的 egg-bin

直接在 scripts 里面加入即可:

{
  "scripts": {
    "test": "egg-bin test",
    "cov": "egg-bin cov"
  },
  "devDependencies": {
    "egg-bin": "^1.9.0"
  }
}
@kazupon
Copy link

@kazupon kazupon commented Dec 15, 2016

FYI:
official released

@oychao
Copy link

@oychao oychao commented Jan 8, 2017

有没有gulp的用法示例~

@atian25
Copy link
Owner Author

@atian25 atian25 commented Jan 8, 2017

@oychao
Copy link

@oychao oychao commented Jan 8, 2017

已经实现了,刚找到这一篇博客,作为一个日常日本IT黑,看到一个如此优秀的JS库的作者居然是个日本人,http://efcl.info/2014/0406/res3809/

@oychao
Copy link

@oychao oychao commented Jan 8, 2017

太舒服,专门写了半个小时错误,就为了看报错结果~

@kaelzhang
Copy link

@kaelzhang kaelzhang commented Jan 11, 2017

刚开始用 ava 就发现,握草好鸟,这是什么鬼,发现原来是 power-assert

@hyj1991
Copy link

@hyj1991 hyj1991 commented Jan 24, 2017

这个大赞啊!

@atian25
Copy link
Owner Author

@atian25 atian25 commented Mar 23, 2017

@mosaic101
Copy link

@mosaic101 mosaic101 commented Mar 24, 2017

赞!好东西,必须用起来

@huangjihua
Copy link

@huangjihua huangjihua commented Mar 28, 2017

真是好东西,赞

@cncoder
Copy link

@cncoder cncoder commented Mar 28, 2017

junit有就好了Hhhh

@chalince
Copy link

@chalince chalince commented Apr 20, 2017

什么时候支持Async/Await呢?

@atian25
Copy link
Owner Author

@atian25 atian25 commented Apr 20, 2017

@Chalin-Shi 支持的啊

@chalince
Copy link

@chalince chalince commented Apr 20, 2017

@atian25 我上午测试了下,报错,我以为不支持呢。难道是因为不支持外部import的文件吗?还是必须要用node version > 7.6.0,或者babel呢。

@atian25
Copy link
Owner Author

@atian25 atian25 commented Apr 20, 2017

  • 这个跟 power-assert 没有关系
  • Node 7.6+ 才支持 async await
  • import 目前 Node 还不支持
  • babel 编译后的文件,不确定是否支持,可以看下官方站点,貌似有插件
  • 不建议后端代码用 babel 编译
@chalince
Copy link

@chalince chalince commented Apr 20, 2017

@atian25 谢谢你。我本地node用的是6.10版本,应该是这个原因了,然后我准备测试时用7.6+,部署是用6.10,不知道这样好不好。。。

@chalince
Copy link

@chalince chalince commented Apr 20, 2017

目前准备报koa2的项目,转到eggjs,感觉文档好全,很不错,貌似next版本才支持Async/Await是吗?

@atian25
Copy link
Owner Author

@atian25 atian25 commented Apr 20, 2017

  • 你可以在测试里面配置 6, 7 两个 runner
  • 1.0 版本支持 async await.
@crycime
Copy link

@crycime crycime commented Apr 23, 2019

babel配合使用不支持怎么解决哈

@atian25
Copy link
Owner Author

@atian25 atian25 commented Apr 23, 2019

babel配合使用不支持怎么解决哈

https://github.com/power-assert-js/power-assert

看官方 README,有 babel 插件

@crycime
Copy link

@crycime crycime commented Apr 23, 2019

巴贝尔配合使用不支持怎么解决哈

解决了

@Suwings
Copy link

@Suwings Suwings commented Nov 2, 2019

确实,赞同 "没有API就是最好的API" 这句话。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.