NodeJS模块中的newman.run不起作用

时间:2016-09-07 10:27:17

标签: node.js gruntjs bluebird newman

我想从node.js模块执行newman,但它不起作用。并且没有显示错误。

从命令行运作良好

newman run postman-tests/collection.json -e postman-tests/environment.json
// newman installed globally for this command

但是下面的节点模块代码不起作用:

var newman = require('newman'),
    Promise = require('bluebird'),
    newmanRun = Promise.promisify(newman.run);

//.. other working grunt task here ...
// added new task postman-test

grunt.registerTask('postman-test','postman task running ',  function() {
    Promise.coroutine(function*() {
        try {
            console.log('test start-----');
            var response = yield newmanRun({
                collection: require('./postman-tests/collection.json'),
                environment: require('./postman-tests/environment.json'),
                reporters: 'cli'
            });
            console.log('run complete-----', response);
        } catch (e) {
            console.log('postman test catch error: ', e);
        }
    })();
});

当我跑步" grunt postman-test"命令仅显示在控制台"test start-----"中,并显示Done, without errors.但没有执行测试

我的代码中有什么问题?任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

默认情况下,grunt会同步处理所有任务注册。由于您忘记调用this.async方法告诉Grunt您的任务是异步的,所以会发生这种情况。为简单起见,Grunt使用同步编码方式,可以通过调用任务正文中的this.async()来切换到异步。 doc link

grunt.registerTask('postman-test', function() {
        var done = this.async();// added this line
        Promise.coroutine(function*() {
            try {
                yield newmanRun({
                    collection: "./postman-tests/0.8/Meed-Services-0.8.postman_collection.json",
                    environment: "./postman-tests/0.8/local.postman_environment.json",
                    reporters: 'cli'
                });
                console.log('*******postman test complete********');
                done();
            } catch (e) {
                console.log('postman test catch error: ', e);
                done(false);
            }
        })();
    });