javascriptnode.jsdom-eventsasync.jspostman-newman

Async.parallelLimit Function Executing Callback Function Before Tasks


I am attempting to setup some newman test runs in parallel for different environments, and then create a Jira ticket via API request once all are completed with the results. However, when I use the ansync.parallelLimit function for that task, I find that the jira ticket gets created without any of the information from the test runs.

A simplified version of the process is this:

var async = require('async');
const path = require('path');
const newman = require('newman');
config = require(path.join(__dirname, 'config.json'));

const jira_data = [];

var create_ticket = function(err, results){
    console.log("Run Complete");
};

var test_run = async() =>{
  newman.run({
    collection: path.join(__dirname, 'Test.postman_collection.json'),
    reporters: ['htmlextra'],
    reporter: {
      htmlextra : { export : path.join(__dirname, '/reports/Test.html')}
    },})
}

var commands = [test_run,test_run,test_run,test_run];

async.parallelLimit(commands,4, (err, results) => {
  console.log("Run Complete");
  console.log(results);
});

That will result in the following output, indicating create_ticket was executed before anything else:

Run Complete
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Created the htmlextra report.
Created the htmlextra report.
Created the htmlextra report.
Created the htmlextra report.

Alternatively, I can make test_run a function instead of async, like this:

var async = require('async');
const path = require('path');
const newman = require('newman');

const jira_data = [];

var create_ticket = function(err, results){
    console.log("Test Run Complete");
};

var test_run = function() {
  newman.run({
    collection: path.join(__dirname, 'Test.postman_collection.json'),
    reporters: ['htmlextra'],
    reporter: {
      htmlextra : { export : path.join(__dirname, '/reports/Test.html')}
    },}).on('request', function(err, results) {
      console.log("Hello");
    })
}

var commands = [test_run, test_run, test_run, test_run];

async.parallelLimit(commands, 4, create_ticket);

But now, create ticket doesn't run at all:

Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Using htmlextra version 1.22.3
Hello
Hello
Hello
Hello
Created the htmlextra report.
Created the htmlextra report.
Created the htmlextra report.
Created the htmlextra report.

After spending more time than I care to admit playing with different combinations of variable types, argument passing, and other fixes, I am completely stumped. Any help at all would be greatly appreciated.


Solution

  • You need to pass and call a callback into test_run to complete the asynchronous task.

    var test_run = function(callback) {
      newman.run({
        collection: path.join(__dirname, 'Test.postman_collection.json'),
        reporters: ['htmlextra'],
        reporter: {
          htmlextra : { export : path.join(__dirname, '/reports/Test.html')}
        },}).on('request', function(err, results) {
          console.log("Hello");
          callback(null);
        })
    }