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

  • SEARCH
  • Home
  • 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 6244361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:18:46+00:00 2026-05-24T12:18:46+00:00

Consider the following jasmine spec: describe(something.act(), function() { it(calls some function of my module,

  • 0

Consider the following jasmine spec:

describe("something.act()", function() {
  it("calls some function of my module", function() {
    var mod = require('my_module');
    spyOn(mod, "someFunction");
    something.act();
    expect(mod.someFunction).toHaveBeenCalled();
  });
});

This is working perfectly fine. Something like this makes it green:

something.act = function() { require('my_module').someFunction(); };

Now have a look at this one:

describe("something.act()", function() {
  it("calls the 'root' function of my module", function() {
    var mod = require('my_module');
    spyOn(mod); // jasmine needs a property name
                // pointing to a function as param #2
                // therefore, this call is not correct.
    something.act();
    expect(mod).toHaveBeenCalled(); // mod should be a spy
  });
});

This is the code I’d like to test with this spec:

something.act = function() { require('my_module')(); };

This has bogged me down several times in the last few months. One theoretical solution would be to replace require() and return a spy created with createSpy(). BUT require() is an unstoppable beast: it is a different “copy” of the function in each and every source file/module. Stubbing it in the spec won’t replace the real require() function in the “testee” source file.

An alternative is to add some fake modules to the load path, but it looks too complicated to me.

Any idea?

  • 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-05-24T12:18:48+00:00Added an answer on May 24, 2026 at 12:18 pm

    It looks like I found an acceptable solution.

    The spec helper:

    var moduleSpies = {};
    var originalJsLoader = require.extensions['.js'];
    
    spyOnModule = function spyOnModule(module) {
      var path          = require.resolve(module);
      var spy           = createSpy("spy on module \"" + module + "\"");
      moduleSpies[path] = spy;
      delete require.cache[path];
      return spy;
    };
    
    require.extensions['.js'] = function (obj, path) {
      if (moduleSpies[path])
        obj.exports = moduleSpies[path];
      else
        return originalJsLoader(obj, path);
    }
    
    afterEach(function() {
      for (var path in moduleSpies) {
        delete moduleSpies[path];
      }
    });
    

    The spec:

    describe("something.act()", function() {
      it("calls the 'root' function of my module", function() {
        var mod = spyOnModule('my_module');
        something.act();
        expect(mod).toHaveBeenCalled(); // mod is a spy
      });
    });
    

    This is not perfect but does the job quite well. It does not even mess with the testee source code, which is kind of a criterion for me.

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

Sidebar

Related Questions

Consider following XML document fragment: <Book> <Title>Example</Title> <Content> Some line </Content> <TOC> Again some
Let's consider following html code: <p> Some text followed by <span>a span element</span> and
Consider following JavaScript code (tested in Firefox): function f(a) { if (a == undefined)
Consider following function definition in ghci. let myF = sin . cos . sum
Consider following text file: something something something = someother thing other thing = third
Consider following piece of code: declare @var bit = 0 select * from tableA
Consider following LINQ-to-NHibernate queries: var q1 = from se in query.ToList<SomeEntity>() where prop1 ==
Consider following code : <p style=margin: 30px 0; padding: 0;>Some text some text some
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };

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.