如何安装和运行Mocha节点。js测试模块?安装后获取“mocha:未找到命令”

时间:2023-01-17 22:00:31

I'm having trouble getting Mocha to work as expected, and I'd love to say as documented, but there (appears) to not be much documentation on actually getting the thing running.

我在让Mocha正常工作上遇到了麻烦,我想说的是有文档记录的,但是关于让它运行的文档似乎不多。

I've installed it using npm (both globally and locally), and each time when I run it I get:

我已经使用npm(全球和本地)安装了它,每次运行它时我都会得到:

$ mocha
mocha: command not found

Ok, so I figured it's not in my PATH, so I tried running it directly,

我想它不在我的路径中,所以我试着直接运行它,

$ ./node_modules/mocha/bin/mocha 
execvp(): No such file or directory

Finally, I tried hitting the other bin file, and got,

最后,我尝试点击另一个bin文件,

$ ./node_modules/mocha/bin/_mocha 
path.existsSync is deprecated. It is now called `fs.existsSync`.

  .

  ✔ 1 tests complete (1ms)

How can I just execute my tests with a single command? Vows seems to let you, but I've heard Mocha is the better choice, I just can't seem to get it working correctly.

如何只用一个命令执行测试?誓言似乎让你,但我听说摩卡是更好的选择,我只是不能让它正常工作。

And any thoughts on the error I got above in my third attempt?

对我第三次尝试中犯的错误有什么想法吗?

Edit:

编辑:

I'm running,

我跑步,

  • Ubuntu 11.10 64-bit
  • Ubuntu 11.10 64位
  • Node.js 0.7.5
  • 节点。js 0.7.5
  • npm 1.1.8
  • npm 1.1.8
  • mocha 0.14.1
  • 摩卡0.14.1
  • should 0.6.0
  • 应该0.6.0

2 个解决方案

#1


147  

you don't want your library tests to fail depending on the machine setup (mocha as global, global mocha version, etc), the way to use the local mocha that works cross-platform is:

您不希望您的库测试因机器设置而失败(mocha作为全局的、全局的mocha版本等),使用本地跨平台的mocha的方法是:

node node_modules/.bin/mocha

npm puts aliases to all the binaries in your dependencies on that special folder. Finally, npm will add node_modules/.bin to the PATH automatically when running an npm script, so in your package.json you can do just:

npm将别名放到您依赖于该特殊文件夹的所有二进制文件中。最后,npm将添加node_modules/。运行npm脚本时,bin自动返回路径,所以在您的包中。只需:

"scripts": {
  "test": "mocha"
}

and invoke it with

和调用它

npm test

#2


16  

After further reading, and confirmation from Linus G Thiel above, I found I simply had to,

在进一步的阅读和以上Linus G Thiel的确认之后,我发现我不得不,

  • Downgrade to Node.js 0.6.12
  • 下调节点。js 0.6.12
  • And either,
    • Install Mocha as global
    • 安装摩卡随着全球
    • Add ./node_modules/.bin to my PATH
    • 添加/ node_modules /。本我的路径
  • 或者,将Mocha安装为global Add ./node_modules/。本我的路径

#1


147  

you don't want your library tests to fail depending on the machine setup (mocha as global, global mocha version, etc), the way to use the local mocha that works cross-platform is:

您不希望您的库测试因机器设置而失败(mocha作为全局的、全局的mocha版本等),使用本地跨平台的mocha的方法是:

node node_modules/.bin/mocha

npm puts aliases to all the binaries in your dependencies on that special folder. Finally, npm will add node_modules/.bin to the PATH automatically when running an npm script, so in your package.json you can do just:

npm将别名放到您依赖于该特殊文件夹的所有二进制文件中。最后,npm将添加node_modules/。运行npm脚本时,bin自动返回路径,所以在您的包中。只需:

"scripts": {
  "test": "mocha"
}

and invoke it with

和调用它

npm test

#2


16  

After further reading, and confirmation from Linus G Thiel above, I found I simply had to,

在进一步的阅读和以上Linus G Thiel的确认之后,我发现我不得不,

  • Downgrade to Node.js 0.6.12
  • 下调节点。js 0.6.12
  • And either,
    • Install Mocha as global
    • 安装摩卡随着全球
    • Add ./node_modules/.bin to my PATH
    • 添加/ node_modules /。本我的路径
  • 或者,将Mocha安装为global Add ./node_modules/。本我的路径