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

rewiring any function executes every line that isn't in any function #183

Open
leopuzzuoli opened this issue May 18, 2020 · 0 comments
Open

Comments

@leopuzzuoli
Copy link

My goal is to unit test the following Node.js script (heavily simplified for readability):

script.js

//setup
console.log("dosomething");
callfunction();

//functions
function callfunction(){
return "function called";
}

with the following mocha / chai test:

/test/test_script.js

let assert = require("assert");

describe("script", () => {
  it("should equal function called", () => {
    assert(callfunction() === "function called");    
 })
})

This obviously doesn't work as callfunction() is not recognized. So i tried using the module "rewire" to obtain the function:

let assert = require("assert");
let rewire = require("rewire"); //tonguetwister right there
let path = require("path");

describe("script", () => {
  it("should equal function called", () => {
    let app = rewire(path.resolve("script.js"));
    callfunction = app.__get__("callfunction");
    assert(callfunction() === "function called");
 })
})

This however, instead of just executing callfunction(), executes the entire script, including the initial console.log("dosomething").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant