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 · 25 comments

Comments

@atian25
Owner

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 from No API is the best API - 抛弃 should.js 吧 to No API is the best API - 抛弃 should/expect/chai 吧 Dec 6, 2016

@fengmk2

This comment has been minimized.

Show comment
Hide comment
@fengmk2

fengmk2 Dec 6, 2016

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

fengmk2 commented Dec 6, 2016

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

@Clarence-pan

This comment has been minimized.

Show comment
Hide comment
@Clarence-pan

Clarence-pan Dec 7, 2016

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

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

@DavidCai1993

This comment has been minimized.

Show comment
Hide comment
@DavidCai1993

DavidCai1993 Dec 7, 2016

这报错体验好到泪目~👍

这报错体验好到泪目~👍

@mlyknown

This comment has been minimized.

Show comment
Hide comment

mlyknown commented Dec 8, 2016

666

@joyeecheung

This comment has been minimized.

Show comment
Hide comment
@joyeecheung

joyeecheung Dec 15, 2016

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

joyeecheung commented Dec 15, 2016

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

@atian25

This comment has been minimized.

Show comment
Hide comment
@atian25

atian25 Dec 15, 2016

Owner

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

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

Owner

atian25 commented Dec 15, 2016

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

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

@atian25

This comment has been minimized.

Show comment
Hide comment
@atian25

atian25 Dec 15, 2016

Owner

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

直接在 scripts 里面加入即可:

{
  "scripts": {
    "test": "egg-bin test",
    "cov": "egg-bin cov"
  },
  "devDependencies": {
    "egg-bin": "^1.9.0"
  }
}
Owner

atian25 commented Dec 15, 2016

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

直接在 scripts 里面加入即可:

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

This comment has been minimized.

Show comment
Hide comment
@kazupon

kazupon Dec 15, 2016

FYI:
official released

kazupon commented Dec 15, 2016

FYI:
official released

@oychao

This comment has been minimized.

Show comment
Hide comment
@oychao

oychao Jan 8, 2017

有没有gulp的用法示例~

oychao commented Jan 8, 2017

有没有gulp的用法示例~

@oychao

This comment has been minimized.

Show comment
Hide comment
@oychao

oychao Jan 8, 2017

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

oychao commented Jan 8, 2017

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

@oychao

This comment has been minimized.

Show comment
Hide comment
@oychao

oychao Jan 8, 2017

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

oychao commented Jan 8, 2017

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

@kaelzhang

This comment has been minimized.

Show comment
Hide comment
@kaelzhang

kaelzhang Jan 11, 2017

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

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

@hyj1991

This comment has been minimized.

Show comment
Hide comment
@hyj1991

hyj1991 Jan 24, 2017

这个大赞啊!

hyj1991 commented Jan 24, 2017

这个大赞啊!

@atian25

This comment has been minimized.

Show comment
Hide comment
@mosaic101

This comment has been minimized.

Show comment
Hide comment
@mosaic101

mosaic101 Mar 24, 2017

赞!好东西,必须用起来

赞!好东西,必须用起来

@huangjihua

This comment has been minimized.

Show comment
Hide comment
@huangjihua

huangjihua Mar 28, 2017

真是好东西,赞

真是好东西,赞

@cncoder

This comment has been minimized.

Show comment
Hide comment
@cncoder

cncoder Mar 28, 2017

junit有就好了Hhhh

cncoder commented Mar 28, 2017

junit有就好了Hhhh

@Chalin-Shi

This comment has been minimized.

Show comment
Hide comment
@Chalin-Shi

Chalin-Shi Apr 20, 2017

什么时候支持Async/Await呢?

什么时候支持Async/Await呢?

@atian25

This comment has been minimized.

Show comment
Hide comment
@atian25

atian25 Apr 20, 2017

Owner

@Chalin-Shi 支持的啊

Owner

atian25 commented Apr 20, 2017

@Chalin-Shi 支持的啊

@Chalin-Shi

This comment has been minimized.

Show comment
Hide comment
@Chalin-Shi

Chalin-Shi Apr 20, 2017

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

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

@atian25

This comment has been minimized.

Show comment
Hide comment
@atian25

atian25 Apr 20, 2017

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

atian25 commented Apr 20, 2017

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

This comment has been minimized.

Show comment
Hide comment
@Chalin-Shi

Chalin-Shi Apr 20, 2017

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

Chalin-Shi commented Apr 20, 2017

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

@Chalin-Shi

This comment has been minimized.

Show comment
Hide comment
@Chalin-Shi

Chalin-Shi Apr 20, 2017

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

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

@atian25

This comment has been minimized.

Show comment
Hide comment
@atian25

atian25 Apr 20, 2017

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

atian25 commented Apr 20, 2017

  • 你可以在测试里面配置 6, 7 两个 runner
  • 1.0 版本支持 async await.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment