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

219
Vistas
How to create a testinstance of my database so i can pass it as a parameter to another module?

I just want a simple mocked database that returns a value when a method is called, but i can't figure out how to do it. I am very new to javascript, so there might be something obvious that i am missing.

What i want is something close to this:

it("stub database", async () => {
      var test = {code: 'test'}
      var fake = sinon.stub(db, 'method')
      fake.returns(test)
      var result = fake.method("anything") // <- function does not exist
      sinon.assert.match(result.code,"test")
    });

But this does not work.

This almost works

it("stub database", async () => {
      var test = {code: 'test'}
      sinon.stub(db, 'method').returns(test)
      var result = db.method("anything")
      sinon.assert.match(result.code,"test")
    });

But if i then send the db to another instance, the stub stops working.

The db instance i want to replace is

module.exports = exports = (app, data = null) => {
    data = data(app) || require('db')(app)
    exports.app = app  
    return exports
}
exports.method = (input) => {
    return data.method(input)
}

I feel like this is the easiest thing ever in any other language, but i have no idea why this is so hard in node, so any help would be very helpful

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