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

350
Views
How to stub an async database call with sinon

I am trying to unit test the getAll method on this class:

module.exports = class BookController {
  static async getAll() {
    return await Book.query()
      .eager('reviews');
  }
};

Book is a Objection model.

How I'd like to test it is I want to fake the response from Book.query().eager() with my own data, since I can never verify what's in the database. For it to be a true unit test, should I just be testing that the Book.query() method is called? Or should I be testing that the returned data since that's the contract of the getAll() method? I'm really unsure how I should be stubbing this out.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

With Sinon > 2.x, you can call .resolves() on your stub. I would do something like

var stub = sinon.stub(BookController, 'getAll');
stub.resolves({response:"ok"});
var bc = new BookController();
bc.getAll.then(function(data){
    expect(data.response).to.equal("ok");
    done();
},function(err){
    done("should NEVER get here");
});
over 4 years ago · Santiago Trujillo Report
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!