0

我有一个问题,包括在 durandal 环境中进行 mocha 测试。我想从命令行使用 mocha-phantomjs 运行我的测试。该测试在浏览器中完美运行,但一旦我尝试使用 mocha-phantomjs,我就会收到以下错误消息(命令:mocha-phantomjs dummyPage.htm):

Error: Your custom reporter tried to require 'fs', but Mocha is not running in N
ode.js in mocha-phantomjs, so Node modules cannot be required - only other repor
ters

我的虚拟 html 页面如下所示:

<script type="text/javascript">
    require.config({
        baseUrl: '../app/',
        paths: {
            'app': '../app',
            'specs': '../sampleTest/specs/',
            'text': '../lib/require/text',
            'durandal': '../lib/durandal/js',
            'plugins' : '../lib/durandal/js/plugins',
            'transitions' : '/lib/durandal/js/transitions',
            'knockout': '../lib/knockout/knockout-2.3.0',
            'jquery': '../lib/jquery/jquery-1.9.1'
        }
    });
    var runTests = function (specfiles) {
        // Initialize mocha and leak assert into the global space.
        mocha.setup('bdd');
        mocha.reporter('html');
        assert = chai.assert;
        require(specfiles, function () {
            require(['specs/test.spec'],function(spec){
                    if (window.mochaPhantomJS) {
                        console.log('test with phantomJS')
                        mochaPhantomJS.run();
                    }
                    else {
                        mocha.run();
                    }
                });
        });
    };
    runTests();
</script>

我的示例测试如下所示:

define(['viewmodels/flickr'], function (flickr) {
    describe('Flickr-Test', function(){
        it('displayName should be equal to Flickr', function () {
            assert.equal(flickr.displayName,'Flickr','should load right view model');
        });
        it('state should be pending', function () {
            assert.equal(flickr.activate().state(),'pending','state should be pending');
        });
    });
    describe("DOM Test", function () {
    var el = document.createElement("div");
    el.id = "myDiv";
    el.innerHTML = "Hello World!";
    document.body.appendChild(el);
    var myEl = document.getElementById('myDiv');

    it("has the right text", function () {
        assert.equal(myEl.innerHTML,'Hello World!')
    });
});
})
4

1 回答 1

0

我没有使用 mocha-phantomjs,但您可能会遇到http://durandaljs.com/documentation/Native-Apps-With-Node-Webkit.html中描述的问题

接下来,因为 node 有它自己的 require 实现,这与 Durandal 使用的 require.js 不同,我们需要稍微修补一下。为此,请将以下脚本块添加到 index.html 的头部:

<script type="text/javascript">
// this doesn't apply when not running with node webkit (nw)
// window.gui = require('nw.gui');

  window.requireNode = window.require;
  delete window.require;

  window.requireNode.version = process.versions.node;
  delete process.versions.node;
</script>
于 2014-04-16T09:13:57.740 回答