Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

215
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?

9 months 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.

9 months ago · Santiago Trujillo Report
Answer question
Find remote jobs