我试图让 nodeunit 模块在 coffeescript 项目中工作,但似乎无法运行基本测试。 这是我的示例 Coffeescript 需要'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 中启动并运行简化测试吗?
提前致谢
最佳答案
您的示例对我来说运行良好。可能是您使用的是旧版本的 nodeunit,在它具有 CoffeeScript 支持之前;尝试
npm install -g nodeunit
更新到最新版本。
如果失败,那么我怀疑这是一个路径问题,因此当 nodeunit 尝试执行 require 'coffee-script'
时,它会失败。
先做
npm install -g coffee-script
并注意输出的最后一行,它应该类似于
coffee-script@1.1.2 /usr/local/lib/node_modules/coffee-script
现在运行
echo $NODE_PATH
在我的例子中是 /usr/local/lib/node_modules
。您需要将 NODE_PATH
设置为 npm 创建的 coffee-script
目录的父目录,方法是添加一行
export NODE_PATH=/usr/local/lib/node_modules
到 ~/.profile
或 ~/.bashrc
或者你的 shell 在启动时运行的任何其他东西,然后重新启动你的 shell。然后,每当您从计算机上的任何 Node 应用程序require 'coffee-script'
时,它都会找到 CoffeeScript 库。
关于node.js - 如何将 node.js 模块 'nodeunit' 与 coffeescript 文件一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8000912/