0

I am a complete beginner with Node.JS and Mocha and was tasked to write unit test's for a group project. My problem is that i do not even know where to start since the return value is a promise. Watching a lot of guides i have learned how to check return values for common functions but it wouldn't help me with a real world example. If any expierienced developer could help me with a guide and code example specific to the function i listed i could hack and understand it and apply to other functions as well. Here is a code example that get's statistics from a CSV File

function getStatistics() {
    return new Promise((resolve, reject)=>{
        try {
            let readStatistics = [];
            const statisticsReadStream = fs.createReadStream(statisticsFileName);
            csv.fromStream(statisticsReadStream, {headers: true})
                .transform(function (data) {
                    data.avgDuration = parseFloat(data.avgDuration);
                    data.avgPassed = parseFloat(data.avgPassed);
                    data.avgReachedPoints = parseFloat(data.avgReachedPoints);
                    data.minReachedPoints = parseInt(data.minReachedPoints);
                    data.maxReachedPoints = parseInt(data.maxReachedPoints);
                    return data;
                })
                .on("data", function (data) {
                    readStatistics.push(data);
                })
                .on("end", function () {
                    resolve(readStatistics);
                    statisticsReadStream.close();
                });

        }catch(err){
            reject();
        }
    });

}
  • 2
    Possible duplicate of [How do I properly test promises with mocha and chai?](https://stackoverflow.com/questions/26571328/how-do-i-properly-test-promises-with-mocha-and-chai) – holydragon May 15 '19 at 01:55
  • if i try the code provided by the article i get an error message: TypeError: Cannot read property 'resolve' of undefined It does not really help me – Nezir Dzanko May 15 '19 at 11:26

2 Answers2

1

In mocha, you can return the promise from the test function to indicate that the test (it) has completed.

describe('My Test', function () {
  it('should do something cool', function () {
    return getStatistics().then(readStatistics => {
      // Assert whatever you want here
    });
  });
});

Any error that gets thrown from your getStatistics function or any assertion error will cause the test to fail.

If you are specifically looking to see if something throws an error, you can catch the error (reject()) in a test too.

describe('My Test', function () {
  it('should do something cool', function () {
    return getStatistics().catch(error => {
      // Assert whatever you want here about the error
    });
  });
});

https://mochajs.org/#asynchronous-code

Andrew Nolan
  • 1,987
  • 2
  • 20
  • 23
  • That is very useful, thank you! I want to check wheter the transformationw orks correctly, however i get an error 1) Statistic should transform data into strings: TypeError: Cannot read property 'transform' of undefined at statGet.then.readStatistics (test.js:42:25) at process._tickCallback (internal/process/next_tick.js:68:7) – Nezir Dzanko May 15 '19 at 13:37
0

Here is the code:

`describe('Statistic', function(){
  it('should transform data into strings', function () {
    return statGet().then(readStatistics => {
      let maybe = statGet();
      var csv = maybe.csv;

      let dat = function (data) {
        data.avgDuration = "1,2";
        data.avgPassed = "2,3";
        data.avgReachedPoints ="3,4";
        data.minReachedPoints = "4";
        data.maxReachedPoints = "5";
        return data;
      }
      assert.typeOf(csv.transform(dat, 'string'));
    });
  });
});`

on the other hand, i have liuttle idea of what i shold be testing in the first place. I feel hopelessly lost. I want to go back to hello world =(