1

I have a large CSV file that contains inputs and expected outputs for a complex calculation. I would like to use this file as the basis for tests of my calculator written in Node. However, it seems that frameworks like Mocha and Vows expect tests to be output synchronously, not asynchronously, after a CSV file has been read and parsed.

I could work around this by converting the CSV to JSON and including it in my test file, but I would rather use the authoritative CSV file as is, and anyway I'm just curious how to handle this situation. Thanks.

Basic approach now (using csvtojson):

    const cases = [];
    csv()
        .fromFile('../testdata/test.csv')
        .on('json', (rowObj) => {
           // convert columns to inputs and expected
           cases.push(inputs: inputs, expected: expected);
        })
        .on('end', () => {
           describe('Test cases', function() {
              cases.forEach((test) => {
                 it(`${dynamicCaseName}`, () => {
                   // do our calculation
                   assert.equals(ours, test.theirs);
                 });
              });
            });
        });
ed94133
  • 1,477
  • 2
  • 19
  • 40

1 Answers1

1

You could just separate the logic of testing and loading entirely, wrap the loader in a promise that blocks the test until the array is populated (very straightforward with async/await if you're using node8, otherwise just Promise.each the results.

If you really dont wanna do that you could Promisify your testing framework

Paulo Black
  • 310
  • 2
  • 10
  • 1
    Paulo, this question has been marked as a duplicate, however if you look at the question it supposedly duplicates, I think your answer is much better. Perhaps you could add your answer there so it benefits those looking to solve this problem? – ed94133 Aug 26 '17 at 01:04
  • Sure I'll add it there as well – Paulo Black Aug 27 '17 at 15:26