摩卡作为图书馆

mocha as a library

本文关键字:图书馆 摩卡      更新时间:2023-09-26

我想使用mocha(node.js测试框架,而不是ruby mocking库)作为库,而不是使用mocha可执行文件来运行我的测试。

有可能用这种方式进行摩卡测试吗?这些例子都只是调用mocha库,假设它们已经是"required",而mocha可执行文件提前完成了所有的"requireing",但我真的更喜欢在我的脚本中显式地执行它们,这样我就可以简单地在脚本上设置+x并直接调用它。

我能做这样的事吗?

#!/usr/bin/env coffee
mocha = require 'mocha'
test = mocha.Test
suite = mocha.Suite
assert = require("chai").assert
thing = null
suite "Logging", () ->
  setup (done) ->
    thing = new Thing()
    done()
  test "the thing does a thing.", (done) ->
    thing.doThing () ->
      assert.equal thing.numThingsDone, 1
      done()
  teardown (done) ->
    thing = null
    done()

这是可能的,但肯定不推荐。

查看mocha二进制文件的源代码(特别是bin/_mocha),了解它的作用。特别是run函数。它使用的所有东西——RunnerReporter等等——都是由mocha库导出的,所以没有什么可以阻止你对它进行逆向工程

此功能已添加。我在下面举了一个例子。

我从这里得到信息:

您将需要2个文件。一个测试,一个运行测试。您可以将runTest标记为可执行文件,并在mocha选项中设置其输出。

runTest.js

#!/usr/bin/env node
var Mocha = require('mocha'),
    fs    = require('fs'),
    path  = require('path');
var mocha = new Mocha(
{
  ui: 'tdd'     
});
mocha.addFile(
  path.join(__dirname, 'test.js')
);
mocha.run(function(failures){
  process.on('exit', function () {
    process.exit(failures);
  });
});

test.js

var assert = require('chai').assert
suite('Array', function(){
  setup(function(){});
  suite('#indexOf()', function(){
    test('should return -1 when not present', function(){
      assert.equal(-1, [1,2,3].indexOf(4));
    });
  });
});

下面的代码片段允许您以编程方式控制Node之外的Mocha主要功能,如添加套件和在不同步骤中运行套件。关键的一点是找出如何使mocha接口全局可访问(该代码也可作为要点)

var Mocha = require("mocha");
var mocha = new Mocha();
var _suites = [];
var _done = false;
/**
 * default mocha options
 * for other configurations, check out bin/_mocha
 */
mocha.reporter("nyan");
mocha.useColors(true);
mocha.growl();
module.exports = {
    /**
     * set interface (bdd is default) and make it global to node
     * @param  {string} interface bdd|tdd|exports|qunit
     */
    init: function(interface) {
        interface && mocha.ui(interface);
        mocha.suite.emit('pre-require', global, undefined, mocha);
    },
    /**
     * add suite
     * @param {function} suite to be executed later
     */
    add: function(suite) {
        mocha.suite && _suites.push(suite) && suite();
    },
    /**
     * run added suites
     */
    run: function(cb) {
        console.info('run mocha');
        var done = function () {
            _done = true;
            cb && cb();
            process.on('exit', function() {
                console.info("exit mocha");
                process.exit(1);
            });
        };
        mocha.run(done);
    },
    /**
     * end mocha test
     */
    exit: function() {
        if (_done) {
            process.exit();
        } else {
            var start = new Date().getTime(),
            interval = setInterval(function() {
                if (_done) {
                    console.log("test suites finished");
                    clearInterval(interval);
                    process.exit();
                } else if (new Date().getTime() - start > 5000) {
                    console.log("wait for nothing!");
                    clearInterval(interval);
                    process.exit();
                }
            }, 250);
        }
    },
    /**
     * change mocha options at runtime, e.g., "reporter", "nyan"
     */
    _set: function(key, val){
        mocha[property](val);
    }
};