【问题标题】:How to ues npm.commands.test function如何使用 npm.commands.test 功能
【发布时间】:2023-04-07 05:38:01
【问题描述】:

我想创建一个可以通过 npm test 返回结果的函数。

(通过调用此函数npm.commands.test(packages, callback)https://docs.npmjs.com/api/test

我试过这样使用:

var npm = require("npm");
npm.load('', function(err, npm) {
  npm.commands.test('package.json', function(data) {
  });
});

它可以运行如命令npm test,但我不知道如何得到结果?回调函数是什么样的?

【问题讨论】:

    标签: javascript node.js testing npm


    【解决方案1】:

    npm.commands.test 执行当前项目的 package.json 文件中定义的测试命令。此外,npm.commands.test 将第一个参数重定向到测试命令。

    示例

    包.json:
    {
        ...
        "scripts": {
            "test": "mocha"
        }
        ...
    }
    
    代码:
    var npm = require('npm');
    ...
    npm.commands.test('myTestDirectory', function(err){
        // if one of more tests failed, err will be set to the string:
        // 'Test failed.  See above for more details.'
        // Else, err will be undefined.
    });
    
    调用:
    /path/to/project/root> mocha "myTestDirectory"
    

    在某些情况下,特别是如果您想执行另一个项目或包的测试,使用child_process 模块生成npm test 的新进程并通过进程的stderr 访问输出可能更有意义和标准输出流或直接调用特定项目的测试运行程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2019-12-11
      • 2014-07-19
      相关资源
      最近更新 更多