jhnns / rewire

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

A behaviour that could have #189

Closed gabrielxfs closed 2 years ago

gabrielxfs commented 3 years ago

Greetings developers of this interesting and useful project,

I have a suggestion to improve/enhance this project. On the path to write unit test for a project, I decided to try write test to the front-end. As the file have a lot of functions that is called in the web context, it is, are declared but not exported, this project is pretty adequate to solve my problem using Jest.

However, the same file have a lot of jQuery call like $(document).ready(function() {, $(document).on( 'click', 'foo-button', function() {, etc. So this generate the error message below:

● Test suite failed to run

ReferenceError: $ is not defined

  76 | };
  77 | 
> 78 | $(document).ready(function() {
     | ^

The behavior that would be desirable, is to bypass function calls of functions that are not defined (as the jQuery example) on that file.

An additional point would be bypass overall instructions.

This two behaviors described above is to help fine tune the main point of this project. That is, calling a var foo = rewire('foo') just for use the foo.__get__ or foo.__set__ to tinker and pass to the test function.

jhnns commented 2 years ago

I'm sorry, you can't rewire function calls in the top-level module scope. In order to rewire a module, it needs to be loaded first. Changing this would be far out of scope of this project.