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

212
Vistas
Mock a function inside a module using Jest

I'm currently having a problem with mocking a function inside another lib.

In our team, we have a project (npm lib, let's call it component-lib) with some components and functions that are being used by our React main app. We expose the functions and components in a single JS file. (index.js) like this:

import { isFeatureEnabled } from './modules/features'
import mockComponent from './components/mockComponent'

export {
isFeatureEnabled,
mockComponent
}

I'm trying to mock the return value of one of the functions inside this component-lib that is being used by the main react app, my unit test goes like this:

PS: I'm using Jest and React Testing Library


describe('unit test', () => {
  jest.mock('component-lib', () => ({
    ...jest.requireActual('component-lib'),
    isFeatureEnabled: jest.fn().mockReturnValue(false)
  }))

  it('Should send data', () => {
     render(<Main />)
     [... i do some stuff here and trigger the isFeatureEnabled function ...]
  })
})

and the code I use it:


  function onSubmitPassword (data) {
    console.log('is this feature enabled? ', isFeatureEnabled('feature')) // logs as true
    if (isFeatureEnabled('feature') {
     ...do this // this is executed
    } else {
      do that
    }
  }

The thing is that when I do a console.log to see the value of isFeatureEnabled, it's returning true instead of false, ignoring the mock.

Do you guys have any idea why the mock is not working in this case?

I already tried importing the lib as:

import * as components from 'component-lib'

jest.mock('component-lib', () => ({
    ...jest.requireActual('component-lib'),
    isFeatureEnabled: jest.fn()
  }))

it('Should send data', () => {
     isFeatureEnabled.mockReturnValue(false)
     render(<Main />)
     [... i do some stuff here and trigger the isFeatureEnabled function ...]
  })

and it returns an error saying the function only has a getter

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