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

120
Views
¿Es posible burlarse de la llamada de función interna?

Imaginemos que tengo un módulo como el siguiente:

 // utils.ts function innerFunction() { return 28; } function testing() { return innerFunction(); } export {testing}

Me gustaría escribir una prueba unitaria para probar las testing y simplemente simular el valor de retorno de innerFunction , esperando que cualquier llamada a innerFunction se resuelva en un cierto valor, algo como a continuación:

 jest.mock('../utils', () => { const originalModule = jest.requireActual('../utils'); return { // __esModule: true, ...originalModule, innerFunction: jest.fn().mockReturnValue(33), }; }); import { testing } from '../utils'; it('should be okay', () => { expect(testing()).toBe(33); });

Esperaba que jest.requireActual pudiera leer todas las funciones e innerFunction: jest.fn().mockReturnValue(33) en realidad causará que cualquier invocación de innerFunction solo devuelva 33 como valor, pero del pequeño experimento anterior parece que es no es el caso.

En la llamada real, la innerFunction devolverá 28 , pero en el entorno Jest me gustaría que la innerFunction pueda resolver cualquier valor que me gustaría

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Opción 1

Intente pasar la función interna como una dependencia de las pruebas.

utils.ts

 function testing(innerFunction: () => number) { return () => innerFunction(); } export {testing}

prueba.ts

 import { testing } from '../utils' it('should be okay', () => { const mock = () => 33 // AKA System under test const sut = testing(mock) expect(sut()).toBe(33); });

Las pruebas pueden ser más simples sin bromas, y también más predecibles...

opcion 2

Mueva la función interna a otro archivo y use jest.mock

funcióninterna.ts

 export function innerFunction() { return 33 }

utils.ts

 import { innerFunction } from './innerFunction.ts' function testing() { return innerFunction(); } export { testing }

prueba.ts

 jest.mock('./innerFunction', () => { return () => 33 }); import { testing } from '../utils' it('should be okay', () => { expect(testing()).toBe(33); });
about 4 years ago · Juan Pablo Isaza Report
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!