'mocha' 未被识别为内部或外部命令 - mocha 未随 supertest 自动安装

'mocha' is not recognized as an internal or external command - mocha not getting automatically installed with supertest

我需要对用 NodeJS 编写的其余 api 进行单元测试。为此,我需要使用 supertest npm package. I was following this tutorial article 来实现单元测试功能。

现在,在本文中提到 mocha 本身已包含在 supertest 中,因此无需单独安装。所以,我只安装 supertest 使用 - npm install --save-dev supertest.

但是,当我在我的 package.json - "test": "mocha" 和 运行 npm test 中添加这一行时,它给出错误 'mocha' is not recognized as an internal or external command。我需要 运行 npm install --save-dev mocha 才能成功 运行。但是为什么我需要单独包含 mocha 如果它应该是 运行,为什么本文或 supertest 的 npm 主页上没有提到它?

我去了 Supertest page on npmjs 并检查了依赖项。 Mocha 被列为开发依赖项。这意味着在创建超测试时使用了 Mocha,但超测试运行不需要 Mocha。 当我在 Node.js 项目上工作时,我安装了某些包作为开发依赖项。这些可以是用于测试 (Mocha) 或 linting (Standard) 的包。这意味着当项目部署时,它不需要这些包来运行。

此外,您可以使用npm list命令查看安装了哪些模块。我刚刚安装了 supertest 和 运行 npm list。这是超级测试的结构:

如您所见,Mocha 未列出

此外,运行 npm ls mocha 输出为: $ npm ls mocha nodetest@1.0.0 E:\Nodetest -- (empty)

当我 运行 npm ls supertest 时,输出是: $ npm ls supertest nodetest@1.0.0 E:\Nodetest -- supertest@3.3.0

为什么文章说 mocha 可用?可能在 supertest 的早期版本中,它作为依赖项包含在内