jhnns / rewire

Easy monkey-patching for node.js unit tests
MIT License
3.08k stars 128 forks source link

Fails to test a function in CLI script #130

Closed amitguptagwl closed 6 years ago

amitguptagwl commented 6 years ago

I have created a node script file

#!/usr/bin/env node

function cli(args){
  :
}
:
cli(process.argv);

Now I want to test it if works fine for provided arguments.

var climodule = rewire(".././index");
var cli = climodule.__get__("cli");
:
cli(["node", "stubmatic", "--version" ,"tobeignored"]);

But it is executing cli function with actual arguments instead of the arguments I passed.

JvJefke commented 6 years ago

Rewire still runs the file on require (like require). This means that the cli function executes twice. Once on initialisation (with process.argv parameters) and once from your test script. Are you sure, you tested the second execution?

amitguptagwl commented 6 years ago

Hmm, It means I'll have to handle the first call in the way it doesn't impact another call.

Thanks for your quick suggestion.