摩卡作为图书馆

时间:2012-03-13 21:55:12

标签: javascript node.js coffeescript mocha

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

是否可以通过这种方式运行摩卡测试?这些例子都只是调用mocha库假设它们已经“require'd”,并且mocha可执行文件提前完成所有“需要”,但我真的更喜欢在我的脚本中明确地执行它们以便我可以简单地在我的脚本上设置+ 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()

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

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

我从here:

获取了信息

您需要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));
    });
  });
});

答案 2 :(得分:0)

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

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);
    }
};