Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
¿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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda