gpt4 book ai didi

node.js - 如何从 node.js 中的一个 "npm test"命令运行 mocha 和 mocha-phantomjs 测试?

转载 作者:IT老高 更新时间:2023-10-28 21:51:38 27 4
gpt4 key购买 nike

我有几个 Node 包可以在 node.js 环境和浏览器中工作。现在我有两个单独的测试(针对每个环境)。仅使用 npm test 命令运行这些测试的最佳方法是什么?我也想将这些包添加到 travis 中。

我正在使用 mochamocha-phantomjs

Node 测试命令

node ./node_modules/mocha/bin/mocha ./test/node/index.js --reporter spec

浏览器测试命令

node ./node_modules/mocha-phantomjs/bin/mocha-phantomjs ./test/browser/index.html

我尝试了什么:

  1. 将这些命令添加到 npm test 脚本中,用分号分隔
    • 问题:当第一个脚本有错误但第二个脚本没有错误时,命令以 0 退出并且 travis 构建通过。
  2. 让 Node 命令在 npm test 脚本中进行测试,并为浏览器测试创建自定义脚本。然后将这两个命令(npm testnpm run-script test-browser)作为数组添加到 travis.yml 中。
    • 问题:用户必须手动运行两个独立的测试脚本。
  3. 让 Node 命令在 npm test 脚本中进行测试,并将浏览器测试添加到 npm posttest 命令中。 Travis.yml 将只有一个脚本,用户还必须运行一个脚本(每个人都很高兴)。
    • 问题:感觉不对,所以想知道有没有更好的办法。

最佳答案

我喜欢以下内容:

  "scripts": {
"test": "npm run test-node && npm run test-browser",
"test-node": "mocha -R spec ./test/node/index.js",
"test-browser": "mocha-phantomjs ./test/browser/index.html"}

&& 仅在第一次通过时运行第二次,如果需要,您可以单独运行。注意 npm 总是使用 relative mocha (在 node_modules 中),而不是全局的,所以直接调用 mochamocha-phantomjs 并没有什么坏处。使用 mocha 的 -b 选项可以提高效率,一旦遇到错误就会退出。

关于node.js - 如何从 node.js 中的一个 "npm test"命令运行 mocha 和 mocha-phantomjs 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20376269/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com