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

使用power-assert代替should/expect/chai #7

Open
waltcow opened this issue Feb 12, 2017 · 0 comments
Open

使用power-assert代替should/expect/chai #7

waltcow opened this issue Feb 12, 2017 · 0 comments

Comments

@waltcow
Copy link
Owner

waltcow commented Feb 12, 2017

在写node的单元测试的时候我们常用的断言库有

user.should.have.property('name', 'test');
user.enabled.should.ok;

expect(5).to.be.a('number');
expect(res.body.data).to.be.deep.equal({});

存在的问题

  • API种类繁多,需要经常翻阅文档
  • 出错时反馈的信息无法准确判断引起的原因,很多时候需要配合log,或者debug的工具

Power Assert in JavaScript

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 不需要记忆
  • 错误反馈信息非常详尽
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
var assert = require('assert');

describe('Array', function(){
    beforeEach(function(){
        this.ary = [1,2,3];
    });
    describe('#indexOf()', function(){
        it('should return index when the value is present', function(){
            var zero = 0, two = 2;
            assert(this.ary.indexOf(zero) === two);
        });
        it('should return -1 when the value is not present', function(){
            var minusOne = -1, two = 2;
            assert.ok(this.ary.indexOf(two) === minusOne, 'THIS IS AN ASSERTION MESSAGE');
        });
    });
});

describe('various types', function(){
    function Person(name, age) {
        this.name = name;
        this.age = age;
    }
    beforeEach(function(){
        this.types = [
            'string', 98.6, true, false, null, undefined,
            ['nested', 'array'],
            {object: true},
            NaN, Infinity,
            /^not/,
            new Person('alice', 3)
        ];
    });
    it('demo', function(){
        var index = this.types.length -1,
            bob = new Person('bob', 5);
        assert(this.types[index].name === bob.name);
    });
});

HOW TO USE

使用 babel-plugin-espower 插件

安装

npm install --save-dev babel-plugin-espower

编辑 .babelrc 文件

{
  "presets": [
    ...
  ],
  "plugins": [
    "babel-plugin-espower"
  ]
}

mocha 运行时通过 babel-register 编译文件

$(npm bin)/mocha --require babel-register test/some_test.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant