如何安装和运行 Node.js 测试模块 Mocha?安装后获取“mocha: command not found”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9722407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:20:38  来源:igfitidea点击:

How do you install and run Mocha, the Node.js testing module? Getting "mocha: command not found" after install

node.jsmocha

提问by Stephen Melrose

I'm having trouble getting Mochato 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,

好的,所以我想它不在我的 . 中PATH,所以我尝试直接运行它,

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

Finally, I tried hitting the other binfile, 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? Vowsseems 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
  • Node.js 0.7.5
  • npm 1.1.8
  • mocha 0.14.1
  • should 0.6.0
  • Ubuntu 11.10 64 位
  • 节点.js 0.7.5
  • 1.1.8 版本
  • 摩卡咖啡 0.14.1
  • 应该 0.6.0

回答by Benja

since npm 5.2.0, there's a new command "npx" included with npm that makes this much simpler, if you run:

从 npm 5.2.0 开始,npm 中包含了一个新命令“npx”,如果您运行:

npx mocha <args>

Note: the optional args are forwarded to the command being executed (mocha in this case)

注意:可选的 args 被转发到正在执行的命令(在这种情况下是 mocha)

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 中选择可执行的“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.

请注意,如果您没有安装 mocha,此命令将自动获取并使用最新版本,这对于某些工具(例如脚手架)非常有用,但对于某些您可能想要的依赖项可能不是最值得推荐的固定到特定版本。

You can read more on npx here

您可以在此处阅读有关 npx 的更多信息



Now, if instead of invoking mocha directly, you want to define a custom npm script, an alias that might invoke other npm binaries...

现在,如果不是直接调用 mocha,而是想定义一个自定义 npm 脚本,一个可能调用其他 npm 二进制文件的别名...

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 会在运行 npm 脚本时自动将 node_modules/.bin 添加到 PATH,所以在你的 package.json 中你可以这样做:

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

and invoke it with

并调用它

npm test

回答by Stephen Melrose

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
  • And either,
    • Install Mocha as global
    • Add ./node_modules/.binto my PATH
  • 降级到 Node.js 0.6.12
  • 或者,
    • 将 Mocha 安装为全局
    • 添加./node_modules/.bin到我的PATH

回答by Ashutosh Jha

For windows :

对于窗户:

Package.json

包.json

  "scripts": {
    "start": "nodemon app.js",
    "test": "mocha"
  },

then run the command

然后运行命令

npm run test

回答by DHEERAJ

While installing the node modules for mocha I had tried the below commands

在为 mocha 安装节点模块时,我尝试了以下命令

  • npm install
  • npm install mocha
  • npm install --save-dev mocha
  • npm install mocha -g # to install it globally also
  • 安装
  • npm 安装摩卡
  • npm install --save-dev mocha
  • npm install mocha -g # 也可以全局安装

and on running or executing the mocha test I was trying

在运行或执行 mocha 测试时,我正在尝试

  • mocha test
  • npm run test
  • mocha test test\index.test.js
  • npm test
  • 摩卡测试
  • npm 运行测试
  • 摩卡测试 test\index.test.js
  • npm 测试

but I was getting the below error as:

但我收到以下错误:

'Mocha' is not recognized as internal or external command

'Mocha' 不被识别为内部或外部命令

So , after trying everything it came out to be just set the path to environment variables under the System Variables as:

因此,在尝试了一切之后,结果只是将系统变量下的环境变量的路径设置为:

C:\Program Files\nodejs\

C:\Program Files\nodejs\

and it worked :)

它奏效了:)