Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Tiempos de ejecución de conteo incorrectos de Sinon Spy

Tengo una función principal que llama a otras 3 funciones. Quiero probar si esas 3 funciones fueron llamadas.

miControlador.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 }

prueba.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(); }); });

Parece extraño, pero de acuerdo con la documentación del nock , usar varios nocks es la forma correcta.

El problema es que siempre devuelve:

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

Puedo ver el mensaje console.log en la consola, pero el recuento sigue siendo 0. ¿Me estoy perdiendo algo aquí?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!