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

261
Vistas
how spy a defined variable in node.js

Please help me! I have been wrecking my mind but I can not find out how should I stub a variable! Am I wrong? Should I use Spy?

How should I test this code

module.exports = async () => {
  var variable = 'something';
  var taskProcessor = require('taskprocessor');
  try {
    taskProcessor(variable).then().catch();
    //blah blah 
    //blah blah
    //blah blah
    //blah blah
  } catch (error) {
    console.log(err);
  }
};
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

First of all, you should know what is stub or spy (I excluded mocks intentionally)

We use doubles to Control a method’s behavior, then change the test direction to cover all paths in our test.

The spy wraps around the function, it does not replace its functionality! But with stub we can define the output. Spy is literally sending an spy inside your enemies (in this case your code :D) to mimic the behavior of a genuine entity and gather information for you!

now let’s go back to your question!

You can use rewire module in this case. From it’s git page

rewire adds a special setter and getter to modules so you can modify their behavior for better unit testing. You may

  • inject mocks for other modules or globals like process
  • inspect private variables
  • override variables within the module.

So you can set any variable like this: enter image description here

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