0

When a query from salesforce comes back as an empty array, we catch that inside of .then() and throw error which I can console.log and see inside of .catch(). However I am having a hard time testing that error message.

I've tried chai-as-promise and to.eventually.equal('some string'), but came back as AssertionError: expected undefined to equal 'No campaigns for current period.'

cosnt campaignMember = {

  getCampaignMembers: async () => {
    await login();
    return conn.sobject('CampaignMember')
      .select('*')
      .then((result) => {
        if (!result[0]) {
          throw Error('No campaigns for current period.');
        }
        return result;
      })
      .catch((err) => {
        log.error(`Could not get paid current campaigns ${err}`);
      });
  },
}
module.exports = campaignMember

TEST

it('should pass', async () => {
    await otherAsyncMethod();
    await expect(campaignMember.getCampaignMembers(currentParent)).to.eventually.equal('No campaigns for current period.');
  });

I want to be able to test the error message itself.

pynexj
  • 19,215
  • 5
  • 38
  • 56
il0v3d0g
  • 655
  • 6
  • 14

1 Answers1

0

I found a solution through another stackoverflow article with this link to github issue comment. https://github.com/chaijs/chai/issues/882#issuecomment-322131680 I also had to remove catch from my async getCampaignMembers method.:

cosnt campaignMember = {

  getCampaignMembers: async () => {
    await login();
    return conn.sobject('CampaignMember')
      .select('*')
      .then((result) => {
        if (!result[0]) {
          throw Error('No campaigns for current period.');
        }
        return result;
      })
      .catch(err => throw Error(err));
  },
}
module.exports = campaignMember

TEST

it('should pass', async () => {
  await otherAsyncMethod();

  await campaignMember. getCampaignMembers(currentParent).catch((err) => {
    expect(err).to.be.an('error').with.property('message', 'Error: No campaigns for current period.');
  });

});
il0v3d0g
  • 655
  • 6
  • 14
  • https://stackoverflow.com/questions/21587122/mocha-chai-expect-to-throw-not-catching-thrown-errors - comment by @ChrisV led me to the answer. fyi – il0v3d0g May 24 '19 at 15:31