记一次使用mocha做单元测试

2,136 阅读2分钟

为什么要使用mocha

我写了一个有向无环图,但是没法验证我写的代码时正确的。所以引入单元测试,选择mocha是因为比较多人用。文档比较全

参考文档

mocha使用

  • 安装mocha
    • yarn add mocha -D
  • 创建test文件夹
  • 编写代码
  • 在package.json中的scripts上添加"test": "mocha --require --require @babel/register src/test"
    • 如果想要在mocha测试文件中使用import语法 需要安装@babel/register(babel-register模块改写require命令,为它加上一个钩子。此后,每当使用require加载.js、.jsx、.es和.es6后缀名的文件,就会先用Babel进行转码, babel-register只会对require命令加载的文件转码,而不会对当前文件转码。另外,由于它是实时转码,所以只适合在开发环境使用。)
代码示例
import { expect } from 'chai';

import chai from 'chai';

const should = chai.should();

import { isDag } from '../utils/IsDAG';

describe('是否是有向无环图测试', function() {
    this.timeout(50000); // 异步请求需要设置超时时间,不然会报超时错误
    it('是Dag测试', function(done) {
        const graph = [
            {
                source: 1,
                target: 2
            },
            {
                source: 1,
                target: 3
            }
        ];
        const result = isDag(graph);

        expect(result).to.equal(true);
        done();
    });
    
    it('异步请求测试', function(done) {
        const graph = [
            {
                source: 1,
                target: 2
            },
            {
                source: 1,
                target: 3
            }
        ];

        setTimeout(() => {
            const result = isDag(graph);

            expect(result).to.equal(true);
            done();
        }, 2000);
        
    });
    
    it('不是Dag测试-没有根节点', function(done) {
        const graph = [
            {
                source: 1,
                target: 2
            },
            {
                source: 2,
                target: 3
            },
            {
                source: 3,
                target: 1
            }
        ];
        const result = isDag(graph);

        result.should.equal(false);
        done();
    });
    
    it('是Dag测试-只有一个点', function(done) {
        const graph = [
            {
                source: 1
            }
        ];
        const result = isDag(graph);
        if (result) {
            done();
        } else {
            done('错误');
        }
    });
});

断言库的使用

  • 使用chai断言库(集合了Assert/Expect和should三种断言库)
  • 安装 yarn add chai
  • 使用expect和should

断言库expect和should对比

1、expect的实现侵入性比较低,expect方法会返回一个代理对象,上面绑定了一些可以用来进行断言的方法。
2、should的实现是将should一类的方法直接注入到所有的对象里,破坏性比较高。如果你自己定义了一些和should一类同名的方法就悲剧了

引用expect和should断言库
var chai = require('chai')
  , expect = chai.expect
  , should = chai.should();
// Or
import { expect } form 'chai';
import chai.should(); form 'chai';
const should = chai.should();

// 使用expect
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);

// 使用should
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);

测试覆盖率

  • nyc
  • Istanbul

nyc使用

  • 安装nyc
    • yarn add nyc -D
    • 在package.json中的scripts上的"test"做修改: nyc mocha --require @babel/register src/test
    • yarn test 就行

模拟浏览器环境

  • Phantom(PhantomJS的功能,就是提供一个浏览器环境的命令行接口,你可以把它看作一个“虚拟浏览器”,除了不能浏览,其他与正常浏览器一样,可以用来做网页爬取)
  • Mocha-phantom
  • 使用Mocha-phantom
    • mocha init 目录名称
    • 修改index.html文件
      if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
      else { mocha.run(); }
      
    • 设置script: "test-phantomjs": "./node_modules/.bin/mocha-phantomjs src/test/IsDagTest/index.html"
    • yarn test-phantomjs

运行测试结果