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

105
Vistas
How to test a service written using Partials

I have the following service and I want to write Unit tests for this service. However, I am exporting a function that is going to return the Service.

My main problem is, to keep things simple at the point where this service will be imported and used, I am using partials to pass in the model as dependency using Partials inside the init method.

const model = require('../models');

const SomeService = (model) => {
  const getSomething = (id) => {
    const [data, error] = model.getSomething(id);
    if (error) {
      return error;
    }
    return data;
  };

  const createSomething = (smt) => {
    const [data, error] = model.createSomething(smt);
    if (error) {
      return error;
    }
    return data;
  };

  const deleteSomething = (id) => {
    const [data, error] = model.deleteSomething(id);
    if (error) {
      return error;
    }
    return data;
  };

  return {
    getSomething,
    createSomething,
    deleteSomething,
  };
};

const init = (model) => () => SomeService(model);

const getSomeService = init(model);

module.exports = getSomeService;

The controller that will be consuming this service may or may not pass some other params but the thing is it definitely should not be passing the model to initialize this service. Hence, I am using the partials.

While writing Unit tests for this service, how do I mock the model? Are there any changes required to make this more testable?

about 4 years ago · Juan Pablo Isaza
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