node.js 创建 package.json 时的测试命令是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44879644/
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 18:19:54  来源:igfitidea点击:

What is the test command while creating package.json?

node.jspackage.json

提问by Aakash Verma

While creating package.jsonfrom command line using npm initfor creating a module in Node.js, there is a test commandfield that I don't know about. There's no mention of it in the docs too on executing npm help jsonalso in the CLI.

在创建package.json使用命令行npm init创建于Node.js的模块,有一个测试命令字段,我不知道。npm help json在 CLI 中执行的文档中也没有提到它。

Please explain what it is about.

请解释它是关于什么的。

采纳答案by Denis Tsoi

The test commandis the command that is run whenever you call npm test.

test command是每当您调用 时运行的命令npm test

This is important when integrating with continuous integration/continuous deployment tools (such as jenkins, codeship, teamcity).

这在与持续集成/持续部署工具(例如jenkinscodeshipteamcity)集成时很重要。

Example:
- say you deploy a project to AWS or some other cloud hosting provider,
- you can set up your infrastructure to automatically run npm test.
- If there are problems within those tests, your ci/cd will automatically rollback before deploying.

示例:
- 假设您将项目部署到 AWS 或其他一些云托管提供商,
- 您可以将基础设施设置为自动运行npm test.
- 如果这些测试中存在问题,您的 ci/cd 将在部署前自动回滚。

To execute tests
You can use karma, jest, or selenium/nightmare/phantomjsor about any other test scripting library/framework that allows you to write and execute tests and then set the required command in scripts.test and finally run it from npm test.

执行测试
您可以使用karmajestselenium/nightmare/phantomjs或任何其他允许您编写和执行测试的测试脚本库/框架,然后在 scripts.test 中设置所需的命令,最后从npm test.

回答by robertklep

Assuming you mean scripts.test:

假设你的意思是scripts.test

"scripts" : {
  "test" : "echo \"Error: no test specified\" && exit 1"
}

This field contains the program(/command line) that should run when you call npm test. Typically, that program is a test-runner like mocha, ava, jest, ...

此字段包含在您调用 时应运行的程序(/命令行)npm test。通常,该程序是一个测试运行程序,如mocha, ava, jest, ...

The default value is a placeholder that prints an error message (try running npm testin the same directory as your package.json).

默认值是打印错误消息的占位符(尝试npm test在与您的 相同的目录中运行package.json)。