0

I have a function, code is written below. I am using lab as code coverage tool.

getClients = async (partnerId) => {
    try {
      const results = await PartnersClients.findAll({
        attributes: ['clientId'],
        where: {
          partnerId,
        },
        include: [{
          model: Models.Entries,
          attributes: ['section'],
        }],
      });
      if (!results.length) return false;
      return results.map(result => ({
        clientId: result.dataValues.clientId,
        section: result.dataValues.Entries.length ? result.dataValues.Entries[0].dataValues.section : '',
      }));
    } catch (error) {
      return false;
    }
  };

I have written tests for it but this line section: result.dataValues.Entries.length ? result.dataValues.Entries[0].dataValues.section : '' shown as not covered. Also on most of the code lines where I have used loops or iterator methods are showing as not covered. I am wondering how should I cover it. als

Rohit Choudhary
  • 2,253
  • 1
  • 23
  • 34

1 Answers1

0

Well, it's kind of odd, I have quite a lot of loops like that and it lab test-report says they are covered.

Here is my code

handler: async (request, h) => {
    try {        
        const results = await Content.find(contentFilters).sort({createdAt: -1})        
            .exec();
        const {simpleResults} = request.query;
        const contentList = results.results.map(row => simpleResults ? row.simpleApiData() : row.apiData());        
        return {
            status: true,
            results: contentList,
        }
    } catch (e) {
        /* $lab:coverage:off$ */
        request.server.log(['error', 'content', 'list'], e);

        return Boom.badRequest(e.message, e);
        /* $lab:coverage:on$ */
    }
}

and here is the test code that covers all the lines.

it('It should list contents', async () => {
    const res = await server.inject({
        url: `/content?${key}=${filters[key]}&page=1`, // I also have another test with additional "simpleResults" parameter added to this url, query parameters validated by Joi ofc. simpleResults: Joi.boolean(),
        method: 'get',
        headers: {
            "Authorization": token
        }
    });

    expect(res.statusCode).to.equal(200);
    expect(res.result.status).to.equal(true);
    expect(res.result.results).to.be.an.array();
    expect(res.result.results).to.have.length(1);
    expect(res.result.results[0].title).to.equal('Game Of Thrones');
});

I have many other complex loops in my code and most of them covered with test. Is is possible to show us your test code, how do you test your conditions?

metoikos
  • 1,315
  • 11
  • 18