Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't seem to be overriding require? #59

Closed
rizowski opened this issue Apr 12, 2015 · 2 comments
Closed

Doesn't seem to be overriding require? #59

rizowski opened this issue Apr 12, 2015 · 2 comments

Comments

@rizowski
Copy link

I am not sure what is going on but I have been trying multiple mocking/require hijacking libraries to see if I can mock some of my dependencies. I like proxyquire but the problem being proxyquire doesn't seem to be giving me my mocked objects.

// item.js
var parser = require('./parser');

module.exports = {
  someFunctionThatUsesParser: function(){ ... };
}
// item-spec.js
var proxy = require("proxyquire"),
  item;
//jasmine before each
beforeEach(function(){
  var mock = {
    'parser' : {
      myMockedFunction: function(){ ... }
    }
  };
  item = proxy("../src/item", mock);
});
// tests below

Folder structure is:

project/
  |- src/
  |--- item.js
  |--- parser.js
  |- spec/
  |--- item-spec.js

Is that not the way this is supposed to be set up? I have checked to see if the parser object that item uses vs what I have mocked are the same object and they are not. Any Ideas?

@bendrucker
Copy link
Collaborator

The keys in the proxyquire call's second arg need to match the requires in your source code.

var mock = {
    './parser' : {
      myMockedFunction: function(){ ... }
    }
  };

@redixhumayun
Copy link

This really needs to be mentioned more explicitly in the documentation that the key needs to be the exact same as the require path in your module.

Repository owner locked as resolved and limited conversation to collaborators Dec 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants