Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda