Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!