如何安装和运行Mocha(Node.js测试模块)?得到"mocha:未找到命令".安装后 [英] How do you install and run Mocha, the Node.js testing module? Getting "mocha: command not found" after install
问题描述
我在使摩卡正常工作时遇到麻烦,我很想说正如所记录的那样,但实际上并没有太多的文献记载.
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.
我已经使用npm
(在全局和本地)安装了它,每次运行它都会得到:
I've installed it using npm
(both globally and locally), and each time when I run it I get:
$ mocha
mocha: command not found
好,所以我发现它不在我的PATH
中,所以我尝试直接运行它,
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
最后,我尝试打另一个bin
文件,并得到了
Finally, I tried hitting the other bin
file, and got,
$ ./node_modules/mocha/bin/_mocha
path.existsSync is deprecated. It is now called `fs.existsSync`.
.
✔ 1 tests complete (1ms)
如何仅用一个命令执行测试? 誓言似乎可以让您,但是我听说Mocha是更好的选择,我似乎无法理解正常工作.
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?
我在跑步,
- Ubuntu 11.10 64位
- Node.js 0.7.5
- npm 1.1.8
- 摩卡咖啡0.14.1
- 应该0.6.0
推荐答案
自npm 5.2.0起,如果运行以下命令,npm附带有一个新命令"npx",它使此操作变得更加简单:
since npm 5.2.0, there's a new command "npx" included with npm that makes this much simpler, if you run:
npx mocha <args>
注意:可选的args会转发到正在执行的命令(在这种情况下为mocha)
Note: the optional args are forwarded to the command being executed (mocha in this case)
这将自动从本地安装的mocha中选择可执行的"mocha"命令(始终将其添加为dev依赖项,以确保您和其他所有人始终使用正确的命令).
this will automatically pick the executable "mocha" command from your locally installed mocha (always add it as a dev dependency to ensure the correct one is always used by you and everyone else).
请注意,如果您未安装mocha,此命令将自动获取并使用最新版本,这对于某些工具(例如脚手架)非常有用,但对于某些依赖项可能不是最可取的可能想固定到特定版本.
Be careful though that if you didn't install mocha, this command will automatically fetch and use latest version, which is great for some tools (like scaffolders for example), but might not be the most recommendable for certain dependencies where you might want to pin to a specific version.
您可以在npx 此处上阅读更多内容. >
You can read more on npx here
现在,如果要定义一个自定义的npm脚本而不是直接调用mocha,则该别名可能会调用其他npm二进制文件...
Now, if instead of invoking mocha directly, you want to define a custom npm script, an alias that might invoke other npm binaries...
您不希望库测试根据机器设置而失败(将Mocha作为全局,全局Mocha版本等),使用跨平台工作的本地Mocha的方法是:
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:
node node_modules/.bin/mocha
npm在该特殊文件夹上的依赖项中的所有二进制文件中都添加了别名. 最后,npm将在运行npm脚本时自动将node_modules/.bin添加到PATH中,因此在package.json中,您可以执行以下操作:
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:
"scripts": {
"test": "mocha"
}
并使用
npm test
这篇关于如何安装和运行Mocha(Node.js测试模块)?得到"mocha:未找到命令".安装后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!