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

285
Vistas
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
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