Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:13:41+00:00 2026-06-05T02:13:41+00:00

I am struggling to write high quality tests around my node modules. The problem

  • 0

I am struggling to write high quality tests around my node modules. The problem is the require module system. I want to be able to check that a certain required module has a method or its state has changed. There seem to be 2 relatively small libraries which can be used here: node-gently and mockery. However, due to their low ‘profile’ it makes me think that either people don’t test this, or there is another way of doing this that I am not aware of.

What is the best way to mock out and test a module that has been required?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:13:43+00:00Added an answer on June 5, 2026 at 2:13 am

    ———– UPDATE —————

    node-sandbox works on the same principals as stated below but is wrapped up in a nice module. I am finding it very nice to work with.


    ————— detailed awnser —————

    After much trial I have found the best way to test node modules in isolation while mocking things out is to use the method by Vojta Jina to run each module inside of a vm with a new context as explained here.

    with this testing vm module:

    var vm = require('vm');
    var fs = require('fs');
    var path = require('path');
    
    /**
     * Helper for unit testing:
     * - load module with mocked dependencies
     * - allow accessing private state of the module
     *
     * @param {string} filePath Absolute path to module (file to load)
     * @param {Object=} mocks Hash of mocked dependencies
     */
    exports.loadModule = function(filePath, mocks) {
      mocks = mocks || {};
    
      // this is necessary to allow relative path modules within loaded file
      // i.e. requiring ./some inside file /a/b.js needs to be resolved to /a/some
      var resolveModule = function(module) {
        if (module.charAt(0) !== '.') return module;
        return path.resolve(path.dirname(filePath), module);
      };
    
      var exports = {};
      var context = {
        require: function(name) {
          return mocks[name] || require(resolveModule(name));
        },
        console: console,
        exports: exports,
        module: {
          exports: exports
        }
      };
    
      vm.runInNewContext(fs.readFileSync(filePath), context);
      return context;
    };
    

    it is possible to test each module with its own context and easily stub out all external dependencys.

    fsMock = mocks.createFs();
    mockRequest = mocks.createRequest();
    mockResponse = mocks.createResponse();
    
    // load the module with mock fs instead of real fs
    // publish all the private state as an object
    module = loadModule('./web-server.js', {fs: fsMock});
    

    I highly recommend this way for writing effective tests in isolation. Only acceptance tests should hit the entire stack. Unit and integration tests should test isolated parts of the system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling to write Makefiles that properly build my unit tests. As an example,
I'm struggling with a problem I'm not able too solve. I've spent a lot
I am struggling to write/configure a ThrowsAdvice interceptor that I want to intercept all
I'm struggling with this problem for a week. I try to write a program
I'm struggling to write a Windows Service that accesses a website, logs in using
I am struggling to write a query to result in the following records. I
I'm trying to write a simple app for my website and struggling with 1
I am struggling to write an AND conditional filter in log4net. Had it been
Hi i'm struggling to write a particular MySQL Join Query. I have a table
I have created a very simple nested loop example and am struggling to write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.