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

184
Visualizações
Wrong count execution times of Sinon Spy

I have a major function that call 3 other functions. I want to test if those 3 function were called.

myController.ts

import axios from 'axios';

export async function functionOne() {
  console.log('Hit functionOne');
  const result = await axios.get('https://www.urlOne.com/first');
  return result;
}

export async function functionTwo() {
  const result = await axios.get('https://www.urlTwo.com/second');
  return result;
}

export async function functionThree() {
  const result = await axios.get('https://www.urlThree.com/third');
  return result;
}

export async function mainFunction() {
  const resultOne = await functionOne();
  // some logic that also calls functionTwo & functionThree
}



test.spec.ts

import * as myCtrl from '../controllers';
import { expect } from 'chai';
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';
import * as chaiAsPromised from 'chai-as-promised';

chai.use(sinonChai);
chai.use(chaiAsPromised);
chai.should();

describe('success', () => {
  const sandbox = sinon.createSandbox();

  // Create Spies
  beforeEach(() => {
    sandbox.spy(myCtrl, 'functionOne');
    sandbox.spy(myCtrl, 'functionTwo');
    sandbox.spy(myCtrl, 'functionThree');
  });

  afterEach(() => {
    sandbox.restore();
  })


  it('test something', async () => {
    nock('https://www.urlOne.com')
      .get('first')
      .reply(200)

    nock('https://www.urlTwo.com')
      .get('second')
      .reply(200)

    nock('https://www.urlThree.com')
      .get('third')
      .reply(200)

    await myCtrl.mainFunction();

    expect(myCtrl.functionOne).to.be.calledOnce();
    expect(myCtrl.functionTwo).to.be.calledOnce();
    expect(myCtrl.functionThree).to.be.calledOnce();
  });
});

It looks weird, but according to the nock documentation, using multiple nocks is the right way.

The problem is, it always returns:

AssertionError: expected functionOne to have been called exactly once, but it was called 0 times

I can see the console.log message on console, but the count still 0. Am I missing something here?

about 4 years ago · Juan Pablo Isaza
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