Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

326
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda