Coder Social home page Coder Social logo

Comments (7)

jhnns avatar jhnns commented on May 22, 2024

rewire adds the setters and getters to the exports-object at the bottom of the module. Just imagine that rewire injects code at the bottom of your module that looks like:

exports.__set__ = function () { ... };
exports.__get__ = function () { ... };

So if you do module.exports = helper, the setters and getters will be attached to the helper-function itself, not to the object returned by calling helper(app).

from rewire.

gprasant avatar gprasant commented on May 22, 2024

Does that mean the only way to be able to use rewire on a module is to
export an object and not export a function ? Do you know of any way to use
rewire with the module pattern ?
On Nov 7, 2013 6:47 PM, "Johannes Ewald" [email protected] wrote:

rewire adds the setters and getters to the exports-object at the bottom
of the module. Just imagine that rewire injects code at the bottom of your
module that looks like:

exports.set = function () { ... }:exports.get = function () { ... };

So if you do module.exports = helper, the setters and getters will be
attached to the helper-object, not to the object returned by calling
helper(app).


Reply to this email directly or view it on GitHubhttps://github.com//issues/22#issuecomment-27961747
.

from rewire.

jhnns avatar jhnns commented on May 22, 2024

rewire still works when exporting a function. If you place your variables you want to mock in the top-level scope of the module, it should be no problem. Given this module:

var http = require("http"),
      fs = require("fs");

function helper(app) {
    return {
        get_watch_list: function () { ... }
        update_watch_list: function (r_ip) { ... }
    };
}

module.exports = helper;

you can still inject mocks for the http- and the fs-module. Just write

var helper = rewire ("./helper");

helper.__set__("http", { ... });
helper.__set__("fs", { ... });

from rewire.

davidposin avatar davidposin commented on May 22, 2024

@gprasant You can but you can only interact with variables at the "top" level. You can't override http.get but you can override the http object to have a custom get function.

I put together a sample on c9 where I override http.get here that might help:
https://c9.io/dposin/jasmine-node-rewire

Also, I have a post that walks through rewire and jasmine-node:
http://randomjavascript.blogspot.com/2013/10/jasmine-node-and-rewire-part-1.html

Hope this helps.

from rewire.

gprasant avatar gprasant commented on May 22, 2024

Thanks @Lastalas .

@jhnns

var helper = rewire ("./helper");

helper.__set__("http", { ... });
helper.__set__("fs", { ... });

# use test_helper for calling functions & user helper for __set__ and __get__ . 
test_helper = helper(app_stub);

This is the style Im going ahead with. Looks good? Better Ideas?

from rewire.

jhnns avatar jhnns commented on May 22, 2024

Looks good! 👍

Thanks @Lastalas for your detailed blog post. Wish I had more time to write such posts...
Just found two things:

  • the link to rewire isn't working
  • you don't need the __dirname-thing in rewire(__dirname + '/server.js'). rewire works just like require and will resolve any relative paths to the current module

from rewire.

davidposin avatar davidposin commented on May 22, 2024

@jhnns I fixed the link. thanks for spotting that.

The __dirname was because of the way c9 handles the actual file structure. I'll look and see if I can remove it.

from rewire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.