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

328
Views
How to inject a stub function when using Hapi.js server.inject

I have a hapijs project which is using the hapi-mongodb plugin.

In the handler I am using the hapi-mongodb plugin to make db calls. See below

internals.getById = async (request, h) => {

    try {

        const db = request.mongo.db;
        const ObjectId = request.mongo.ObjectID;

        const query = {
            _id: ObjectId(request.params.id)
        };

        const record = await db.collection(internals.collectionName).findOne(query);

        //etc.....


I want to be able to test this using server.inject(), but I am not sure how to stub the request.mongo.db and the request.mongo.ObjectID

it('should return a 200 HTTP status code', async () => {

        const server = new Hapi.Server();

        server.route(Routes); //This comes from a required file

        const options = {
            method: 'GET',
            url: `/testData/1`
        };

        //stub request.mongo.db and request.mongo.ObjectID
        
        const response = await server.inject(options);

        expect(response.statusCode).to.equal(200);
    });

Any ideas?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I worked this out and realised that the mongo plugin decorates the server object which can be stubbed.

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!