cleishm / jsmockito

Javascript mocking framework inspired by the awesome mockito
http://jsmockito.org
Other
106 stars 19 forks source link

JsMockito polluting global space with JsHamcrest #35

Open fcuenca opened 9 years ago

fcuenca commented 9 years ago

I installed jsmockito and jshamcrest as separate npm packages, and I found that jshamcrest matchers weren't working when verifying mocks. Matchers were working perfectly fine in other assertions, however.

After some debugging, I figured that the problem was that I had two conflicting versions of jshamcrest: v0.7, which I installed, and v0.6.7, which jsmockito installs as dependency. When jsmockito first loads, it creates a global JsHamcrest variable which points to it's private version of JsHamcrest (0.6.7). When my code later requires jshamcrest, the newer version is loaded, so when I refer to matchers, they are from a different library than that jsmockito is using, and therefore the objects are not recognized as matchers, and not used.

I solved the problem creating a small utility module that loads both libraries and replaces the global library:

     var jshamcrest = require('jshamcrest').JsHamcrest;
     var jsmockito = require("jsmockito").JsMockito;

    global.JsHamcrest = jshamcrest;

    jsmockito.Integration.importTo(global);

Perhaps the jsmockito module could avoid polluting the global namespace, or relax the version number declared for the dependency (currently set to match exactly 0.6.7)