0

I am running some test scripts. I am using csvtoparse to convert the csv file into json and i am attempting to export the object sets using module.exports. My code is shown below:

const csv = require('csvtojson');

async function getData(){
    const jsonArray=await csv().fromFile('./test1.csv');
    return jsonArray;
}
module.exports = {
    getData: getData
}

However, I am getting this error in my terminal

  ●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "[
      {
        TESTCASE_ID: '1',
        BROWSER_TYPE: 'chromium',
        BASE_URL: 'https://css-tricks.com/'
      },
      {
        TESTCASE_ID: '2',
        BROWSER_TYPE: 'chromium',
        BASE_URL: 'https://css-tricks.com/'
      }
    ]".

Is there a way to iterate through the csv and export each json object in nodejs?

SWarr
  • 139
  • 2
  • 9
  • 2
    How you are calling this method. Can you please share that part? – harpreet cheema Jul 26 '20 at 19:59
  • i am calling it like this `const data = require('./constants'); console.log(await data.getData());` For now i am just trying to log the data – SWarr Jul 26 '20 at 20:01
  • (async function(){console.log(await data.getData());})(); try with this – harpreet cheema Jul 26 '20 at 20:08
  • 1
    There is no issue with the export. This should work just fine. However it does look like you're not running the code directly as `const data = require('./constants'); console.log(await data.getData());` but you're trying to run it in a test framework instead. You need to tell your test framework that you are waiting for something. Most test frameworks allow you to pass a callback as an argument to wait for asynchronous events. Some test framework allow you to return a promise. I don't know which you are using – slebetman Jul 26 '20 at 20:08

0 Answers0