I have a nodejs module with a class.
Inside of the the class there is a method which calls newman (Postman cli runner)
Can't figure out the way how to return newman run result data. The newman call itself (outside of module) works without any problem.
mymodule.js
var newman = require('newman');
module.exports = function (collection, data) {
this.run = function () {
newman.run({
collection: require(this.collection + '.postman_collection.json'),
environment: require(this.environment + '.postman_environment.json')
}, function () {
console.log('in callback');
}).on('start', function (err, args) {
}).on('beforeDone', function (err, data) {
}).on('done', function (err, summary) {
});
return 'some result';
}
}
index.js
var runNewman = require('./mymodule');
var rn = new runNewman(cName, cData);
var result = rn.run(); // never returns any variable
cosole.log(result);
As you can see newman
using events and callback. If you want the data you need to send the data from the done
event callback. What you can do here is convert your code to use Promise api
.
Refer below snippet
var newman = require('newman')
module.exports = function (collection, data) {
this.run = function () {
return new Promise((resolve, reject) => {
newman.run({
collection: require(this.collection + '.postman_collection.json'),
environment: require(this.environment + '.postman_environment.json')
}, function () {
console.log('in callback')
}).on('start', function (err, args) {
if (err) { console.log(err) }
}).on('beforeDone', function (err, data) {
if (err) { console.log(err) }
}).on('done', function (err, summary) {
if (err) { reject(err) } else { resolve(summary) }
})
})
}
}
Calling code for this is
var runNewman = require('./mymodule');
var rn = new runNewman(cName, cData);
var result = rn.run().then(console.log, console.log); //then and catch