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

136
Vistas
How to test an internal function

I'm trying to create a unit test to cover an internal function

fileA.js

module.exports.functionA = () {
  
  const functionB = () {
    // do something
  }

  functionB()
}

test.js

const { functionA } = require('fileA')

...

it('runs functionB', () => {
  functionA()
  expect(...).toHaveBeenCalled()
}

How do I access it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are two possibilities here (your situation looks like the first one, but is also clearly simplified for the purposes of the question).

Either:

  • functionB is entirely private to functionA, part of its implementation, which means you can't access it to test it (directly). Instead, test functionA, which presumably uses functionB as part of the work it does (otherwise, there's no point in an entirely private function in functionA).

    or

  • functionA exposes functionB in some way (for instance, as a return value, or as a method on a returned object, or by setting it on an object that's passed in, etc.), in which case you can get it in whatever way functionA provides and then test it.

(Again, I think yours is the first case.)

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