-
Notifications
You must be signed in to change notification settings - Fork 128
Open
Description
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").
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels