10

この質問は、node.js に関する以前の経験の欠如に基づいている可能性がありますが、jasmine-node がコマンド ラインからジャスミン仕様を実行できることを望んでいました。

TestHelper.js:

var helper_func = function() {
    console.log("IN HELPER FUNC");
};

my_test.spec.js:

describe ('Example Test', function() {
  it ('should use the helper function', function() {
    helper_func();
    expect(true).toBe(true);
  }); 
});

これらは、ディレクトリ内の 2 つのファイルのみです。次に、私がするとき:

jasmine-node .

私は得る

ReferenceError: helper_func is not defined

これに対する答えは簡単だと思いますが、非常に単純なイントロや、github で明白なものは見つかりませんでした。アドバイスや助けをいただければ幸いです。

ありがとう!

4

2 に答える 2

16

ノードでは、すべてがその js ファイルに名前空間化されています。関数を他のファイルから呼び出せるようにするには、TestHelper.js を次のように変更します。

var helper_func = function() {
    console.log("IN HELPER FUNC");
};
// exports is the "magic" variable that other files can read
exports.helper_func = helper_func;

次に、 my_test.spec.js を次のように変更します。

// include the helpers and get a reference to it's exports variable
var helpers = require('./TestHelpers');

describe ('Example Test', function() {
  it ('should use the helper function', function() {
    helpers.helper_func(); // note the change here too
    expect(true).toBe(true);
  }); 
});

最後にjasmine-node .、ディレクトリ内のすべてのファイルを順番に実行すると思いますが、ヘルパーを実行する必要はありません。代わりに、それらを別のディレクトリに移動する (および を正しいパスに変更する./)require()か、単に を実行することができますjasmine-node *.spec.js

于 2012-04-10T02:41:52.607 に答える