Estoy usando Jest para probar algunas funciones de utilidad.
mi_util.js:
export function myFunc(i) { if (i === 1){ anotherFunc(); } }otro_util.js:
export function anotherFunc(i) { console.log('in anotherFunc'); } ¿Cuál es la forma más sencilla de probar que se llamó a anotherFunc() ? Aquí está mi prueba de unidad actual, que está fallando. ¿Hay alguna manera de probar que una función fue llamada por su nombre?
import { myFunc } from './my_util.js' ... it('myFunc should call anotherFunc', () => { const anotherFunc = jest.fn(); myFunc(1); expect(anotherFunc).toHaveBeenCalled(); });Resultados:
Expected number of calls: >= 1 Received number of calls: 0Tal vez debería simplemente inyectar anotherFunc a myFunc como argumento, facilitará las pruebas:
function myFunc(i, cb) { if (i === 1){ cb(); } } it('myFunc should call anotherFunc', () => { const anotherFunc = jest.fn(); myFunc(1, anotherFunc); expect(anotherFunc).toHaveBeenCalled(); });