Mocha + RequireJS = AMD测试 [英] Mocha + RequireJS = AMD testing

查看:142
本文介绍了Mocha + RequireJS = AMD测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将Mocha连接到基于RequireJS的应用程序,可能是你能够想出一些东西:)。经过几个小时,我一直在尝试加载AMD模块,只是在console.log中发布了一些已经发布的信息,模块已被加载......没有发生任何事情 - 程序刚刚结束并打印出一些摩卡信息。

I have a hard time connecting Mocha to RequireJS based application, may be You'll be able to come up with something :). After many hours when I've been trying to load AMD modules and simply console.log some 'fired' info that the module has been loaded... nothing happend had happened - the program just ended and printed out some mocha info.

var facade = requirejs(['../../public/js/scripts/widgets/widgets/article/main.js'],      
    function(mod) {
        console.log('fired')
});
// run with: $ mocha -u tdd test.js --reporter spec

而且我想出了解决回调问题的想法:

and than I've come up with the idea to fire just this to test callbacks:

setTimeout((function() {
    console.log('fired');
}), 5000);
// run with: $ mocha -u tdd test.js --reporter spec

也没用。所以最后我用

$ node test.js 

最后它起作用了。所以问题不仅仅是:如何使用回调处理运行Mocha测试,因为这些对于AMD测试至关重要?

and finally it worked. So question is than: How to run Mocha test with callbacks handling, as those are essential for AMD testing?

推荐答案

你的方式,mocha不会对你的文件做任何事情,因为它没有看到测试套件。 RequireJS计划调用回调,但是在有机会发生之前,mocha会退出。与您的超时示例相同。

The way you are doing it, mocha is not going to do anything with your file because it does not see a test suite in it. RequireJS is scheduled to call the callback but mocha exits before this has a chance to happen. Same with your timeout example.

以下为您提供示例。

文件 test.js

'use strict';
var requirejs = require("requirejs");
requirejs.config({
    baseUrl: '.',
    nodeRequire: require
});

suite('Something', function(){
    var foo;

    suiteSetup(function (done){
        // This saves the module foo for use in tests. You have to use
        // the done callback because this is asynchronous.
        requirejs(['foo'],
                  function(mod) {
            console.log("fired!");
            foo = mod;
            done();
        });
    });

  suite('blah', function(){
    test('blah', function(){
      if (foo.test !==  "test")
          throw new Error("failed!");
    });
  });
});

文件 foo.js

define(function () {
   return {test: "test"};
});

当你跑步时:

mocha -u tdd test.js

你会看到回调被解雇并且测试通过了。

You'll see that the callback is fired and the test passes.

为了让人们阅读这个问题并使用 suite 而感到困惑, suiteSetup test ... Mocha支持多个接口。这里的代码使用TDD接口(OP调用Mocha与 -u tdd ),导出 suite suiteSetup test 等。在默认的BDD界面中,等价物是 describe 分别在之前

For the benefit of people reading this question and confused by the use of suite, suiteSetup, test... Mocha supports multiple interfaces. The code here is using the TDD interface (the OP invokes Mocha with -u tdd), which exports suite, suiteSetup, test, etc. In the default BDD interface, the equivalents are describe, before and it, respectively.

这篇关于Mocha + RequireJS = AMD测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆