Run a method after handleSummary()

I’m trying to upload the file generated from k6-reporter to thru an API using a POST method inside handleSummary().

Although code that does the POST it’s after the one that generates the summary.html, it isn’t created until run it’s completely over, making the POST upload previous test summary.html file.

Is there any way I can somehow run the method after the file is already created?

This is the code from the test script.

import { UpdateTestRail,CheckThresholds } from './tools/tools.js'
import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js';
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";


  export function handleSummary(data) {
      const reqFailed = CheckThresholds(data, 'http_req_failed')
      const reqDuration = CheckThresholds(data, 'http_req_failed')
      var success

      if(reqFailed && reqDuration){
        success = true
      }else{
        success = false
      }

      return {
          'tools/summary.html': htmlReport(data),
          'stdout': textSummary(data, { indent: ' ', enableColors: true}),
          'logs/DataGridTest2.json':(UpdateTestRail(success, 3333, 729, data)),
      }

  }

And a part of the code from UpdateTestRail method that contains the POST with the attachment

let res = http.post(`${host}/index.php?/api/v2/add_results_for_cases/${runId}`,JSON.stringify(body),params);

    const resultId = res.json()[0].id
    const fd = new FormData();
    fd.append('attachment', http.file(summary, 'summary.html', 'text/html'));


    let attachmentParams = {
        headers: {
            Authorization: `Basic ${encodedCredentials}`,
            'Content-Type': 'multipart/form-data; boundary=' + fd.boundary,
        }
    };
    let attachment = http.post(`${host}/index.php?/api/v2/add_attachment_to_result/${resultId}`,fd.body(),attachmentParams);
    return JSON.stringify(data);

The file is declared in tools.js using:

var summary = open('./summary.html', 'b')

Hi @yarielfk, welcome to the community :tada:

Is there any way I can somehow run the method after the file is already created?

You can’t, but also you don’t need to.

htmlReport(data) already returns what you need to upload. So you can just put it in a variable, do your post request and then set it to tools/summary.html in the return.

Hope this helps you!

1 Like

It totally did!

Thanks a lot