2011-11-03 59 views
7

我試圖讓nodeunit模塊在coffeescript項目中工作,但似乎無法運行基本測試。 這裏是我的榜樣CoffeeScript的 需要 'nodeunit'如何在coffeescript文件中使用node.js模塊的'nodeunit'

test = true 
test2 = false 

exports.testSomething = (test) -> 
    test.expect(1) 
    test.ok(true, "this should pass") 
    test.done() 

exports.testSomethingElse = (test2) -> 
    test2.expect(1) 
    test2.ok(false, "this should fail") 
    test2.done() 

不幸的是,當我運行 '$ nodeunit example.coffee' 我得到的錯誤輸出:

example.coffee:4 exports.testSomething = (test) -> ^

module.js:296 throw err; ^SyntaxError: Unexpected token > at Module._compile (module.js:397:25) at Object..js (module.js:408:10) at Module.load (module.js:334:31) at Function._load (module.js:293:12) at require (module.js:346:19) at /usr/local/lib/node/nodeunit/lib/nodeunit.js:75:37 at /usr/local/lib/node/nodeunit/deps/async.js:508:13 at /usr/local/lib/node/nodeunit/deps/async.js:118:13 at /usr/local/lib/node/nodeunit/deps/async.js:134:9 at /usr/local/lib/node/nodeunit/deps/async.js:507:9

誰能幫助我剛剛得到了簡化使用Node.js測試並運行在Coffeescript中?

在此先感謝

回答

14

您的示例對我運行良好。在使用CoffeeScript支持之前,可能會使用舊版本的nodeunit;嘗試

npm install -g nodeunit 

更新到最新版本。

如果失敗了,那麼我懷疑這是一個路徑問題,所以當nodeunit試圖執行require 'coffee-script'時,它會失敗。

首先做

npm install -g coffee-script 

,並注意輸出的最後一行,這應該是這個樣子

[email protected] /usr/local/lib/node_modules/coffee-script 

的現在運行

echo $NODE_PATH 

這在我的情況是/usr/local/lib/node_modules。你需要像

export NODE_PATH=/usr/local/lib/node_modules 

設置NODE_PATH到故宮創建coffee-script目錄的父目錄中加入一行~/.profile~/.bashrc或其他任何這是你的shell運行啓動時,並重新啓動你的shell。然後,無論何時從計算機上的任何Node應用程序執行require 'coffee-script',它都會找到CoffeeScript庫。

+0

現象偵探工作。我的$ NODE_PATH是空的,但我的其他模塊都不在意。非常感謝你。 –

+0

@ user1028416然後,您應該將他的答案標記爲正確。當你在這裏時,考慮給答案+1。我們在Stack Overflow上工作。 :) –

0

如果您的咖啡腳本版本是1.7或更高版本,您可能也會患上bug #247,我剛剛爲此提交了patch

相關問題