2017-08-15 4 views
1

ブラウザで非同期にモカモジュールをロードできますか?私はチャイと共にそれを確かにすることができます。 mochaをamdのようなスタイルで動作させるための回避策はありますか? firstchai作業細かい上記のサンプルコードでmochajsを非同期で実行する(AMDのような)

require.config({ 
    baseUrl: "/scripts", 
    paths: { 
     "mocha": "framework/mocha", 
     "chai": "framework/chai", 
     "first": "custom/first" 
    } 
}); 

require(['first', 'mocha', 'chai'], function (first, mocha, chai) { 
     first.echo(); 

     console.log('something'); 
     console.log('something'); 

     mocha.ui('tdd'); 
     var assert = chai.assert; 

     suite('"Home" Page Tests', function() { 
      test('page should contain link to contact page', function() { 
       assert($('a[href="/contact"]').length); 
      }); 
     }); 

     mocha.run(); 

     console.log('whatever'); 
    }); 

mochaは未定義です。

答えて

1

モカはAMD対応ではありませんので、RequireJSを使用してMochaをロードする場合は、shimが必要です。ここでは、最小限の例が含まれているindex.htmlファイルは次のとおりです。

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/> 
    <link href="node_modules/mocha/mocha.css" type="text/css" media="screen" rel="stylesheet" /> 
    <script type="text/javascript" src="node_modules/requirejs/require.js"></script> 
    </head> 
    <body> 
    <div id="mocha"></div> 
    <script> 
     require.config({ 
      paths: { 
       mocha: 'node_modules/mocha/mocha', 
      }, 
      shim: { 
       mocha: { 
       exports: "mocha", 
       } 
      }, 
     }); 

     require(["mocha"], function (mocha) { 
     mocha.setup("bdd"); 
     it("foo", function() {}); 
     mocha.run(); 
     }); 
    </script> 
    </body> 
</html> 

あなたはindex.htmlが配置されている同じディレクトリにnpm install requirejs mochaを実行しておく必要があります。

shim: { 
     mocha: { 
      exports: "mocha", 
      init: function() { return {mocha: mocha, Mocha: Mocha }; } 
     } 
    }, 
:私はこの shim代わりに使用 Mochaコンストラクタを使用することができるようにしたい場合には

関連する問題