0

I need to publish my Karma test results to a custom REST API. To handle this automatically, I've written a custom Karma reporter. I'm trying to use the run_complete event so that the POST happens after all browsers finish. However, no HTTP call is being made.

I'm using Axios 0.19.2 to do the actual HTTP call, but the same thing happens with node-fetch. The tests are being run by the Angular cli via ng test. My Karma config is lengthy but other than having a million different reporters and possible browser configs, is pretty much standard.

This is my onRunComplete method:

self.onRunComplete = function () {
  var report = ... ; // logic to generate a JSON object, not relevant
  var url = '...'; // the endpoint for the request
      
  try {
    console.log('Sending report to ' + url);
    axios.post(url, report, {headers: {'Content-Type': 'application/json'}})
      .then(function(response) { 
        console.log('Success!');
        console.log(response);
      })
      .catch(function(error) {
        console.log('Failure!');
        console.log(error);
      });
  } catch (err) {
    console.log('Error!');
    console.log(err);
  }
}

At the end of the test run, it writes to console the 'Sending report to...' message, and then immediately ends. The server does not receive the request at all.

I also tried adding explicit blocking using a 'inProgress' boolean flag and while-loop, but that pretty much just leaves the entire test run hanging since it never completes. (Since the request is never made, the 'inProgress' flag is always true and we never hit the then/catch promise handlers or the catch block.)

I have verified that the Axios POST request works by taking the entire contents of the onRunComplete as shown here, putting it in its own JS file, and calling it directly. The report logs as expected. It's only when I call from inside of Karma that it's somehow blocked.

Since Karma's documentation pretty much boils down to "go read how other people did similar things!" I'm having trouble figuring out how to get this to work. Is there a trick to getting an HTTP request to happen inside of a custom reporter? Why does my implementation not work?

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
  • I faced a similar problem, but with an actual browser (not within karma). I wanted to make a http call when the browser was closed (in the onbeforeunload). But what was happening was when the browser is closed, it cancels all xhrs. To avoid this, I used a synchronous xhr (https://stackoverflow.com/questions/59021445/angular-8-beforeunload-event-event-not-work-on-close-tab/59021763#59021763). Not sure if this would help you, but from your description, it seems to be similar. – buchipper Jul 06 '20 at 17:48

1 Answers1

-1

Looks like the post request is made asynchronously - that is the request is made and control resumes almost immediately to the method which completes... try instead:

self.onRunComplete = function () {
  var report = ... ; // logic to generate a JSON object, not relevant
  var url = '...'; // the endpoint for the request
      
  try {
    console.log('Sending report to ' + url);
    await axios.post(url, report, {headers: {'Content-Type': 'application/json'}})
   ...
 }
}
Rob Evans
  • 2,822
  • 1
  • 9
  • 15
  • No, the post request is not made at all. Regardless of whether I await or chain the promise, it never executes. A logging statement after the 'post' never executes -- which, if it were being made asynchronously, would be the case. I also mentioned using an 'in progress' to block while the request was in progress, which looped indefinitely because the issue is that there is no HTTP post being made at all. – Roddy of the Frozen Peas Jul 13 '20 at 15:41
  • how about adding an interceptor to the request? `axios.interceptors.request.use(config => { console.log('Request sent'); return config; }, error => { return Promise.reject(error);});` to the line before the request. If this logs the console message successfully you'll see the log message. If not you may get a clue as to whats wrong with the request your attempting? – Rob Evans Jul 13 '20 at 23:46