How to assert/expect if a function is called in Chai

Issue

I have a function like below which needs to be test with chai

export class GroupMessage {
 public async createTenant(): Promise<void> {
     const groupCreator = [];

     groupCreator.push(ServiceWrapper.Create(project));
     await Promise.all(groupCreator);
 }
  
}  

To test this, I have the below test case

  it('createTenantshould return create tenant', async () => {
    sandbox.stub(ServiceWrapper, 'Create');


    const groupNtmMessage = new GroupMessage();
    GroupMessage.createTenant();
    expect(ServiceWrapper, 'Create').to.have.been.called;
  });

With the above code I get the below error

Error: Invalid Chai property: called. Did you mean "all"?

Any way we can test this, I have looked through chai documentation, but haven’t found anything

Solution

I found the answer to this. Instead of expect, sinon.assert can be used like below


sinon.assert.calledWith(ServiceWrapper.Create, 'test-project');

Answered By – mak15

Answer Checked By – Dawn Plyler (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.