from ruanyifeng

git repo demo

summary

1. npm i -g mocha (then can use everywhere,it's good)
3. npm i D chai mocha mochawesome
2. write test code
    var add = require('./add.js')
    var expect = require('chai').expect;
    describe('add method test',function(){
        it('1+1=2',function(){
            expect(add(1,1)).to.be.equal(2);
        })
    })

3. mocha dir/*
    then dir/* will run ,and get mocha report

hooks

4 type :before,after,beforeEach,afterEach
describe('hooks', function() {

    before(function() {
        // 在本区块的所有测试用例之前执行
    });

    after(function() {
        // 在本区块的所有测试用例之后执行
    });

    beforeEach(function() {
        // 在本区块的每个测试用例之前执行
    });

    afterEach(function() {
        // 在本区块的每个测试用例之后执行
    });

    // test cases
    it('1+1=2',function(){
        expect(1+1).to.be.equal(2);
    })

    it('1+1!=2',function(){
        expect(1+1).to.be.not.equal(2);
    })
});

how to write code

// equal or not equal
expect(4+5).to.be.equal(9);
expect(4+5).to.be.not.equal(9);
expect(foo).to.be.deep.equal({bar:'baz'});

//bool
expect('everthing').to.be.ok;
expect(false).to.not.be.ok

//typeof
expect('test').to.be.a('string);
expect({foo:'baz'}).to.be.an('object');
expect(foo).to.be.aninstanceof(Foo);

//include
expect([1,2,3]).to.include(2);
expect('foobar').to.contain('foo');
expect({foo:'bar',hello:'universe'}).to.include.key('foo');

//empty
expect([]).to.be.empty
expect('').to.be.empty;
expect({}).to.be.empty;

//match
expect('foobar').to.be.match(/^foo/);

results matching ""

    No results matching ""