mochajs 测试框架

326 阅读1分钟

mochajs是一流行的测试框架,可以在node和浏览器执行。提供hook函数,支持异步测试(function(done),done()表示通知mochajs测试完成, async function),用only,skip来管理。代码风格为:

var assert = require('assert');
describe('Array', function() {
    describe('#indexOf()', function() {

        before(function () {
            console.log('before:');
        });

        after(function () {
            console.log('after.');
        });

        beforeEach(function () {
            console.log('  beforeEach:');
        });

        afterEach(function () {
            console.log('  afterEach.');
        });

        it('should return -1 when the value is not present', function() {
          assert.equal([1, 2, 3].indexOf(4), -1);
        });
        
        it('#async with done', (done) => {
            (async function () {
                try {
                    let r = await hello();
                    assert.strictEqual(r, 15);
                    done();
                } catch (err) {
                    done(err);
                }
            })();
        });
        
        it('#test GET /', async () => {
            let res = await request(server)
                .get('/')
                .expect('Content-Type', /text\/html/)
                .expect(200, '<h1>Hello, world!</h1>');
        });
        
        it.only('1 加 1 应该等于 2', function() {
          expect(add(1, 1)).to.be.equal(2);
        });
        
        it.skip('1 加 1 应该等于 2', function() {
          expect(add(1, 1)).to.be.equal(2);
        });
        
  });
});