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

223
Views
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
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!