0

I'm trying to use the promised-csv module (https://www.npmjs.com/package/promised-csv) to read the rows of a CSV file to an array of strings for a unit test:

const inputFile = '.\\test\\example_result.csv';
const CsvReader = require('promised-csv');

function readCSV(inputFile){
   var reader = new CsvReader();
   var output = [];
   reader.on('row', function (data) {
       //console.log(data);
       output.push(data[0]);
   });

  reader.read(inputFile, output);
  return output;
}

I would like to call this function later in a unit test.

it("Should store the elements of the array", async () => {
   var resultSet = readCSV(inputFile);
   console.log(resultSet);
});

However, resultSet yields an empty array. I am also open to use any other modules, as long as I can get an array of strings as a result.

0

2 Answers 2

2

The code should look something like this, according to the docs.

const inputFile = './test/example_result.csv';
const CsvReader = require('promised-csv');

function readCSV(inputFile) {
    return new Promise((resolve, reject) => {

        var reader = new CsvReader();
        var output = [];

        reader.on('row', data => {
            // data is an array of data. You should
            // concatenate it to the data set to compile it.
            output = output.concat(data);
        });

        reader.on('done', () => {
            // output will be the compiled data set.
            resolve(output);
        });

        reader.on('error', err => reject(err));

        reader.read(inputFile);

    });
}

it("Should store the elements of the array", async () => {
    var resultSet = await readCSV(inputFile);
    console.log(resultSet);
});
Sign up to request clarification or add additional context in comments.

3 Comments

I assume so from the documents, but I didn't test it, so I can't be sure. I will test the code myself and update shortly.
Based on my testing with a sample .csv file, simply using CsvReader.prototype.read(), does not return a Promise with the expected data. You must use the EventEmitter interface as is defined in npmjs.com/package/promised-csv#using-the-eventemitter-interface, and return your own promise with the compiled data set.
Made a few updates to take into consideration how the data is presented in the 'row' event.
0

readCSV() returns a Promise. There are two ways that you can access the data it returns upon completion.

  1. As Roland Starke suggests, use async and await.

var resultSet = await readCSV(inputFile); This will wait for the Promise to resolve before returning a value.

More here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

  1. Use Promise.prototype.then() - this is similar to async/await, but can also be chained with other promises and Promise.prototype.catch(). The most important thing to remember is that the function passed to .then() will not be executed until readCSV() has resolved.

readCSV().then((data)=>{return data}).catch((err)=>{console.log(err)})

More here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.