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

282
Views
Jest test is throwing TypeError: is not a function when calling function with Require

I am using Jest to test and cover my express js code. I have a file default.js which is exporting a simple function:

async function foo(req, res) {
  let out = {}
  // performs some computations

  res.send({
    file: 'Default',
    data:  out.data
  })
}

module.export = {foo}

This is then being called in app.js:

const def = require('./default')

app.get('/default/:a/:b' async (req, res) => {
  def.foo(req, res)
})

This works fine. However, when I try to create a test on the Handler function in default.js Jest throws this error:

TypeError: def.foo is not a function

This is my test file:

const def = require('./default')

describe('Default Test', function() {

  test('responds to /default/:a/:b', () => {
    const req = {
      params: {
        a: 'country',
        b: 'city'
      }
    }

    const res = {
      text: '',
      send: function(input) {
        this.text = input
      }
    }

    def.foo(req, res)

    expect(res.json).toEqual({
        file: 'Default',
      })
  })
})

Never mind that the test should fail with the expect function. But the test is failing at default.foo it is not finding this function. Why is this?

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