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

224
Visualizações
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 à 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