2011-11-03 50 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

相关问题