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

337
Views
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 answers
Answer question

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 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!