Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

319
Visualizações
Tesing javascript function which calls another function that instantiates a new object - Sinon, mocha

In a nodejs application I am trying to write a test for a function which calls another function that builds a new object and then a method of that built object is called.

This is the code, service.js, I am trying to test;

// service.js
class Rectangle {
  constructor(height, width) {
      this.height = height;
      this.width = width;
  }

  calcArea(){
      return this.height * this.width
  }
}

const rectBuilderFunc = (height, width) => new Rectangle(height, width);
 
const functionToBeTested = (height, width) => {
    const rect = rectBuilderFunc(height, width);
    const area = rect.calcArea();    
    return area;
}
 
module.exports ={
    functionToBeTested,
    rectBuilderFunc
}

I am using sinon, mocha and chai in my test classes. I am trying to assert that I am calling the builder function with required parameters and in the end a rectangle object is built and its calculate method is called. I end up a test similar to this one;

const expect = require('chai').expect;
const sinon = require('sinon');
const service = require('./service');

describe('test rectangle', () => {
  it('should build a rectangle and calculate the area', (done) => {
      //given
      const rectangleMock = {
          calcArea: sinon.stub()
      };

      service.rectBuilderFunc.withArgs(4,5).returns(rectangleMock);

      //when
      service.functionToBeTested(4,5);

      //then
      expect(service.rectBuilderFunc).to.be.calledOnce;
      expect(service.rectBuilderFunc).to.be.calledWith(4,5);
      expect(rectangleMock.calcArea).to.be.calledOnce;
      expect(rectangleMock.calcArea).to.be.calledWith(4,5);
  });
});
   

The problem is I keep getting rectBuilderFunc is not a function error. Any ideas to have a passing test are appreciated. Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That error is happening because rectBuilderFunc isn't a stub yet where you've called .withArgs here:

service.rectBuilderFunc.withArgs(4,5).returns(rectangleMock);

Instead, you need something like:

const rectBuilderStub = sinon.stub(service, 'rectBuilderFunc');
rectBuilderStub.withArgs(4,5).returns(rectangleMock);

Sinon stub docs: Docs. .withArgs() section is about 1/4 the way down.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda